These are chat archives for rust-lang/rust

10th
Dec 2016
Paul Betts
@paulcbetts
Dec 10 2016 00:20
I'm deeply failing with understanding ownership. I want to write a function on a Trait that returns a Future<Vec<u8>> that is intended to be owned by the caller
How do I do that?
Here's what I've got
pub type ErrFuture<T> = futures::Future<Item = T, Error = std::io::Error>;

pub trait BlobCache<'a> {
  fn get(key: &str) -> ErrFuture<&'a mut Vec<u8>>;
}
I read this as, "A Future of an owned reference to a byte array"
i.e. the person who gets the future now is responsible for that reference
Jay Looney
@zenware
Dec 10 2016 00:21
@paulcbetts These exercises helped me when I was struggling with something similar the other day: https://github.com/carols10cents/rustlings#move-semantics
I still don't know how anything in Rust works well enough to explain it
Ross
@breadmenace_twitter
Dec 10 2016 00:22
@paulcbetts I’m not particularly comfortable with ownership, either, but do you perhaps just want to return a plain Vec<u8> instead of a reference?
Paul Betts
@paulcbetts
Dec 10 2016 00:22
I get "error[E0277]: the trait bound futures::Future<Item=&'a mut std::vec::Vec<u8>, Error=std::io::Error> + 'static: std::marker::Sized is not satisfied"
note: futures::Future<Item=&'a mut std::vec::Vec<u8>, Error=std::io::Error> + 'static does not have a constant size known at compile-time
I feel like this isn't true because isn't a reference a constant size?
i.e. pointer-sized
Also, calling lifetimes an "advanced Rust feature" is kinda misleading imho
Because like, it seems impossible to write any non-trivial library without it
@breadmenace_twitter If I return just a Vec<u8>, Future<T,E> is definitely not a constant size known at compile time
Ross
@breadmenace_twitter
Dec 10 2016 00:27
yeah, I realized that after you’d elaborated, my b
Paul Betts
@paulcbetts
Dec 10 2016 00:27
nbd
Maybe I want a Box here?
Ross
@breadmenace_twitter
Dec 10 2016 00:45
I think so, yeah, but unfortunately I don’t have the Rust chops to tell you for sure
Are you using the nightly compiler? I’m using it and the example you gave builds for me
Paul Betts
@paulcbetts
Dec 10 2016 00:48
I'm using 1.12.1
The trait itself compiles, but when I try to make a struct that implements it, it blows up
https://github.com/akavache/akavache-rs/tree/master/src if you want to look at the whole code
boats
@withoutboats
Dec 10 2016 03:24
@paulcbetts the reference is Sized, but the trait object itself is not
Yea, the issue here is that you're trying to return a bare trait object
You need to make ErrFuture<T> a Box<Future<...>> instead
I am also surprised that the trait declaration compiles, not sure how these checks work but they can behave pretty wonky right now unfortunately
xialvjun
@xialvjun
Dec 10 2016 09:47
fn main() {
    let a = &&vec![5];
    println!("{}", a[0]);
}
Why the code still works right? A pointer pointing to another pointer, we can access it directly ?
Jonas Platte
@jplatte
Dec 10 2016 10:09
I guess deref coercion also happens for the [] operator..