These are chat archives for rust-lang/rust

Aug 2018
Roman Proskuryakov
Aug 07 2018 00:37 UTC
ouch ><'
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
TIL: traits can provide default method defihnitions
Vesa Kaihlavirta
Aug 07 2018 06:33 UTC
@jbg might Arc/Rc help there?
or would that be too inefficient
but would you please tell why would you need it
Denis Lisov
Aug 07 2018 06:56 UTC
@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.
What is fastest, easyiest way to add new key an Value to a json serde Value, that is an object?
I think this > json.as_object_mut().unwrap().insert(...)
Denis Lisov
Aug 07 2018 08:02 UTC
@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?
I am sure
I check it before
Anyway, another Q, what is the Rust eq for :std::os::raw::c_uchar ?
Denis Lisov
Aug 07 2018 09:57 UTC
The documentation says it's always an alias for u8
Aug 07 2018 10:14 UTC
So I guess platform with CHAR_BIT != 8 are not supported
Akos Vandra
Aug 07 2018 10:25 UTC
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
Aug 07 2018 10:38 UTC
@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
Aug 07 2018 10:42 UTC

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
Aug 07 2018 10:45 UTC
Does nightly and stable share the same codebase? Because I see feature gates scattered in stable, but can’t activate them
Andrey Lesnikov
Aug 07 2018 10:47 UTC
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
Aug 07 2018 10:47 UTC
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.
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
Aug 07 2018 11:03 UTC
mm, can you give example?
@ozkriff basically I want a u8 vec to turn into String
Denis Lisov
Aug 07 2018 11:11 UTC
Where does the type come from?
From an service, it an base64 byte arr
per spec
String::from_utf8 seems like diff thing, for decoding
David Vo
Aug 07 2018 11:13 UTC
a CStr and a String are two very different things
Im confused :D
Denis Lisov
Aug 07 2018 11:17 UTC
Is __ptr_arr_item_op pointing to a nul-terminated string?
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
Aug 07 2018 11:21 UTC
Hmm. If that's not a secret, what are you working on?
@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
Aug 07 2018 11:29 UTC
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.
@tanriol after data collected, I run some fn on C
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
@tanriol but in the end of the day I do need a string, like "c2RhYWRzYWRhZHNhZA=="
Vesa Kaihlavirta
Aug 07 2018 12:12 UTC
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
Aug 07 2018 12:14 UTC
@trsh Do you really need the string or just the binary data encoded in it?
Now I have to really think about it :D
@tanriol and what If I go for binary data?
whats the workflow
Denis Lisov
Aug 07 2018 12:16 UTC
Because, IIUC, when you ask GSOAP for the data as a xsd_base64Binary, it decodes that data from base64
Basically it returns and document, encoded as base64
I guess I need the document in the end of the day
Denis Lisov
Aug 07 2018 12:20 UTC
Take a look at std::slice::from_raw_parts, it seems to be what you need.
ok tnx, will look later
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
Aug 07 2018 13:55 UTC
Yes, they do :-)
Note that tt is an "untyped" specifier, which can be reinterpreted later...
Sylwester Rąpała
Aug 07 2018 14:39 UTC
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.
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
Aug 07 2018 17:40 UTC
@qq00 you can look at lalrpop -- is "workspace/members" the list of all crates ?
Aug 07 2018 17:59 UTC
how to run th result of cargo build?
Dylan DPC
Aug 07 2018 18:03 UTC
cargo run? (if you are on a binary)
Is there a way in Cargo.toml to specify that I want to use nightly ?
Lyle Mantooth
Aug 07 2018 19:55 UTC
@qq00, not there, but with rustup override set nightly in that project's directory.
Sylwester Rąpała
Aug 07 2018 20:57 UTC
@qq00 or you can specify it when call cargo: cargo +nightly build