These are chat archives for rust-lang/rust

7th
Aug 2018
Roman Proskuryakov
@kpp
Aug 07 2018 00:37
ouch ><'
qq00
@qq00
Aug 07 2018 01:42
fn foo<T>(v: &[T]) -> &[u8] {
    // I am okay with using unsafe
    // I am okay with using transmute

    // input: _n_ elements of T
    // output: _n * size_of<T>()_ elements of u8
}
How do I implement the above? I tried using transmute, but I end up going from n elements of T to n elements of u8, even when sizeof(T) > 1
I've got a rust-postgres Connection and a Statement<'conn> returned from the .prepare() method of that Connection. I can't for the life of me work out how I can cache the Statement. I need to convince the compiler that the lifetime of the Statement will be at least the lifetime of the Connection. It would seem simple to have one struct which owns both, but I can't figure out any way to write the lifetimes such that this can work.
I think this devolves to the "struct that owns something and also contains a reference to that something" problem which I've hit before
qq00
@qq00
Aug 07 2018 05:57
TIL: traits can provide default method defihnitions
Vesa Kaihlavirta
@vegai
Aug 07 2018 06:33
@jbg might Arc/Rc help there?
or would that be too inefficient
but would you please tell why would you need it
Denis Lisov
@tanriol
Aug 07 2018 06:56
@qq00 Can you guarantee there's no padding in T or [T]?
@jbg There are crates that allow for some limited kinds of self-referential structures, AFAIK, but it requires unsafe.
trsh
@trsh
Aug 07 2018 07:03
What is fastest, easyiest way to add new key an Value to a json serde Value, that is an object?
trsh
@trsh
Aug 07 2018 07:26
I think this > json.as_object_mut().unwrap().insert(...)
Denis Lisov
@tanriol
Aug 07 2018 08:02
@trsh Why are you sure it's an object?
Or, asking it another way, is it correct for your program to panic if it's not an object?
trsh
@trsh
Aug 07 2018 09:33
I am sure
I check it before
Anyway, another Q, what is the Rust eq for :std::os::raw::c_uchar ?
Denis Lisov
@tanriol
Aug 07 2018 09:57
The documentation says it's always an alias for u8
Zakarum
@omni-viral
Aug 07 2018 10:14
So I guess platform with CHAR_BIT != 8 are not supported
Akos Vandra
@axos88
Aug 07 2018 10:25
Hi, I’m a bit confused about the release channels, and how to choose. What are the drawbacks on using nightly for end-applications (not libraries), other than upgrading the version of the compiler may sometimes break code? How often does that happen btw? Right now i’m looking at vec_remove_item, so I don’t have to implement my own.
Michal 'vorner' Vaner
@vorner
Aug 07 2018 10:38
@axos88 Nightly has larger chance of containing bugs and features can, in theory, disappear even after they were stabilized on nightly ‒ though it happens only if something serious happens. I use nightly for development and have stable on CI.
Andrey Lesnikov
@ozkriff
Aug 07 2018 10:42

How often does that happen btw?

It heavily depends on what features are you using. Some features may live without breaking changes for years, some may be changed quite frequently or removed instantly.

Akos Vandra
@axos88
Aug 07 2018 10:45
Does nightly and stable share the same codebase? Because I see feature gates scattered in stable, but can’t activate them
Andrey Lesnikov
@ozkriff
Aug 07 2018 10:47
They use the same codebase, but yep, you can't use feature gates in stable - that's the point of this channel
Michal 'vorner' Vaner
@vorner
Aug 07 2018 10:47
Yes, they are compiled from the same code base, with different flags. Or, the stable is branched from whatever nightly/master there's at the time and may get some bugfix backports before it is released. But otherwise, the code is intact and identical.
trsh
@trsh
Aug 07 2018 10:50
tnx @tanriol
Guys.. any ideas how to handle u8 ptr?
    |
121 |                         CStr::from_ptr(__ptr_arr_item_op).to_str().unwrap().to_owned()
    |                                        ^^^^^^^^^^^^^^^^^ expected i8, found u8
    |
    = note: expected type `*const i8`
               found type `*mut u8`
Andrey Lesnikov
@ozkriff
Aug 07 2018 11:03
mm, can you give http://play.rust-lang.org example?
trsh
@trsh
Aug 07 2018 11:11
@ozkriff basically I want a u8 vec to turn into String
Denis Lisov
@tanriol
Aug 07 2018 11:11
Where does the type come from?
trsh
@trsh
Aug 07 2018 11:12
From an service, it an base64 byte arr
per spec
String::from_utf8 seems like diff thing, for decoding
David Vo
@auscompgeek
Aug 07 2018 11:13
a CStr and a String are two very different things
trsh
@trsh
Aug 07 2018 11:15
Im confused :D
Denis Lisov
@tanriol
Aug 07 2018 11:17
Is __ptr_arr_item_op pointing to a nul-terminated string?
trsh
@trsh
Aug 07 2018 11:19
the bindgen struct looks like fallows
pub struct xsd__base64Binary {
    pub __ptr: *mut ::std::os::raw::c_uchar,
    pub __size: ::std::os::raw::c_int,
So I offset the Pointer by __size, and I get u8 parts
But I duno what todo with them, In the end I need a string
Denis Lisov
@tanriol
Aug 07 2018 11:21
Hmm. If that's not a secret, what are you working on?
trsh
@trsh
Aug 07 2018 11:22
@tanriol Im using GSOAP c lib to access Wsdl services
Works good so far
The Gsoap generates C classes by WSDL, and then I take what I need with Bindgen
In C
struct xsd__base64Binary {
        unsigned char *__ptr;
        int __size;
Denis Lisov
@tanriol
Aug 07 2018 11:29
You should check that yourself, but I believe that __ptr points to an already base64-decoded array, so you need to convert it to a slice/vector, not a string.
I don't yet understand who is to free that buffer, however.
trsh
@trsh
Aug 07 2018 11:39
@tanriol after data collected, I run some fn on C
gsoap_free
That cleans data
like unsafe { free_soap(soap) };
If I call it before collecting data on Rust side, I can see stuff going to hell, So it's cleaning memory
trsh
@trsh
Aug 07 2018 11:48
@tanriol but in the end of the day I do need a string, like "c2RhYWRzYWRhZHNhZA=="
Vesa Kaihlavirta
@vegai
Aug 07 2018 12:12
well one thing is that Rust strings are guaranteed utf8
so the only way to get a Rust string from a bytestream is some form of “from_utf8” function
because bytestreams are obviously not guaranteed to be utf8
Denis Lisov
@tanriol
Aug 07 2018 12:14
@trsh Do you really need the string or just the binary data encoded in it?
trsh
@trsh
Aug 07 2018 12:16
Now I have to really think about it :D
@tanriol and what If I go for binary data?
whats the workflow
?
Denis Lisov
@tanriol
Aug 07 2018 12:16
Because, IIUC, when you ask GSOAP for the data as a xsd_base64Binary, it decodes that data from base64
trsh
@trsh
Aug 07 2018 12:17
Basically it returns and document, encoded as base64
I guess I need the document in the end of the day
Denis Lisov
@tanriol
Aug 07 2018 12:20
Take a look at std::slice::from_raw_parts, it seems to be what you need.
trsh
@trsh
Aug 07 2018 12:29
ok tnx, will look later
qq00
@qq00
Aug 07 2018 13:43
I am comparing Rust macros to Lisp macros.
It seems like Rust macros, for input, is "typed" i.e. :expr, or :ident, :tt
It seems like Rust macros has a "regex like DSL" for pattern matching the input.
Denis Lisov
@tanriol
Aug 07 2018 13:55
Yes, they do :-)
Note that tt is an "untyped" specifier, which can be reinterpreted later...
Sylwester Rąpała
@xoac
Aug 07 2018 14:39
This will be simple question. I wrote a code inside tokio::run(lazy(|| {/*a lot of code*/}) and now I would like to change it to tokio::run(process_data(here_some_variables)); The reason is that I want to use actix_web. And I want to split my program into 2 "main" functions. One for data_process and second for REST API. In main I will have local state variables used by both rest_api and process_data.
qq00
@qq00
Aug 07 2018 17:36
I want to have a single directory with multiple crates. What is a good directory structure to copy? (looking for a suggestion of a project on github, that perferably has a clean+intuitive crate structure) [ I am familiar with how module hierarchy works, but no idea about crates]
Dylan DPC
@Dylan-DPC
Aug 07 2018 17:40
@qq00 you can look at lalrpop
qq00
@qq00
Aug 07 2018 17:45
https://github.com/lalrpop/lalrpop/blob/master/Cargo.toml -- is "workspace/members" the list of all crates ?
tandrysyawaludin
@tandrysyawaludin
Aug 07 2018 17:59
how to run th result of cargo build?
Dylan DPC
@Dylan-DPC
Aug 07 2018 18:03
cargo run? (if you are on a binary)
qq00
@qq00
Aug 07 2018 19:25
Is there a way in Cargo.toml to specify that I want to use nightly ?
Lyle Mantooth
@IslandUsurper
Aug 07 2018 19:55
@qq00, not there, but with rustup override set nightly in that project's directory.
Sylwester Rąpała
@xoac
Aug 07 2018 20:57
@qq00 or you can specify it when call cargo: cargo +nightly build