These are chat archives for rust-lang/rust

2nd
Dec 2017
TatriX
@TatriX
Dec 02 2017 15:33
What is the rust way of doing something like this: https://ideone.com/dck6eb
Aleksey Kladov
@matklad
Dec 02 2017 15:37
@TatriX trait objects will work fine for this use-case
Or, are you interested specifically in sharing width and height fields? For that, I think just composing struct Dimensions(f64, f64) would work
TatriX
@TatriX
Dec 02 2017 15:39
So composition + trait objects. A bit more boilerplate, but maybe less inheritance problems. Thanks :)
Aleksey Kladov
@matklad
Dec 02 2017 15:48

Note that with trait objects you can do cool tricks like storing all triangles in one vector, all rectangles in another vector, and then stack-allocating a trait object for processing.

That is, trait objects don't neccesary force heap allocation

TatriX
@TatriX
Dec 02 2017 15:54
Looks like ECS.
Sean
@seanr707
Dec 02 2017 19:39
anyone on who can help with this lifetime error?
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
   --> src/main.rs:147:46
    |
147 |             let new_messages: Vec<T> = match serde_json::from_slice(new_data) {
    |                                              ^^^^^^^^^^^^^^^^^^^^^^
    |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 146:33...
   --> src/main.rs:146:33
    |
146 |           transfer.write_function(|new_data| {
    |  _________________________________^
147 | |             let new_messages: Vec<T> = match serde_json::from_slice(new_data) {
148 | |                 Ok(data) => data,
149 | |                 Err(e) => {
...   |
159 | |             Ok(new_data.len())
160 | |         }).unwrap();
    | |_________^
note: ...so that reference does not outlive borrowed content
   --> src/main.rs:147:69
    |
147 |             let new_messages: Vec<T> = match serde_json::from_slice(new_data) {
    |                                                                     ^^^^^^^^
    = note: but, the lifetime must be valid for the static lifetime...
note: ...so that types are compatible (expected serde::Deserialize<'_>, found serde::Deserialize<'static>)
   --> src/main.rs:147:46
    |
147 |             let new_messages: Vec<T> = match serde_json::from_slice(new_data) {
Zakarum
@omni-viral
Dec 02 2017 19:40
serde_json::from_slice uses serde::Deserialize
It borrows data from bytes
There should be one that uses serde::DeserializeOwned
Probably serde_json::from_reader
Denis Lisov
@tanriol
Dec 02 2017 19:42
Maybe just add to the bounds T: DeserializeOwned?
Zakarum
@omni-viral
Dec 02 2017 19:43
@tanriol good idea
Sean
@seanr707
Dec 02 2017 19:47
@tanriol Where would I find DeserializedOwned? serde::DeserializedOwnded does not appear
Denis Lisov
@tanriol
Dec 02 2017 19:47
DeserializeOwned, check it for typos :-)
Sean
@seanr707
Dec 02 2017 19:48
where T: serde::DeserializeOwned + Clone is what I have
Denis Lisov
@tanriol
Dec 02 2017 19:49
Sorry, it's not reexported, serde::de::DeserializeOwned
Sean
@seanr707
Dec 02 2017 19:49
Found it. Was `serde::de::DeserializedOwned
Sean
@seanr707
Dec 02 2017 19:56
@tanriol @omni-viral Thank you both!!
David Harvey-Macaulay
@alteous
Dec 02 2017 20:12
Is there a way of applying #[doc(inline)] globally?
i.e. to remove all 're-exports' sections in favour of listing them in the module
Michal 'vorner' Vaner
@vorner
Dec 02 2017 20:47
Hello. I have a crate with a feature and an example that uses that feature. How can I explain to cargo to not try compiling the example if the feature is not turned on? When I run cargo test --no-default-features, it fails on compailing that example
Denis Lisov
@tanriol
Dec 02 2017 20:48
Does #![cfg(feature="name")] on the top of the example work?
Michal 'vorner' Vaner
@vorner
Dec 02 2017 20:49
It now fails with „main function not found“
I probably could wrap the whole thing and provide a dummy, empty main in case the feature is not on, but that kind of beats the purpose of an example to be simple to read 😇