lifetime

What does the first explicit lifetime specifier on an impl mean?

我与影子孤独终老i 提交于 2021-02-06 10:13:45
问题 There are three different lifetime specifiers on an impl: impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } Type<'a> states that there is a lifetime in this impl declaration. The one on the return type -> &'a u32 states that the variable that receives the return value should not die before...before what? Before the object of type Type dies? What's the difference to this: impl TextEditor { //Other methods omitted ... pub fn get_text<'a>(&'a self) -> &'a String { return &self

What does the first explicit lifetime specifier on an impl mean?

血红的双手。 提交于 2021-02-06 10:05:38
问题 There are three different lifetime specifiers on an impl: impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } Type<'a> states that there is a lifetime in this impl declaration. The one on the return type -> &'a u32 states that the variable that receives the return value should not die before...before what? Before the object of type Type dies? What's the difference to this: impl TextEditor { //Other methods omitted ... pub fn get_text<'a>(&'a self) -> &'a String { return &self

What does the first explicit lifetime specifier on an impl mean?

故事扮演 提交于 2021-02-06 10:04:24
问题 There are three different lifetime specifiers on an impl: impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } Type<'a> states that there is a lifetime in this impl declaration. The one on the return type -> &'a u32 states that the variable that receives the return value should not die before...before what? Before the object of type Type dies? What's the difference to this: impl TextEditor { //Other methods omitted ... pub fn get_text<'a>(&'a self) -> &'a String { return &self

Is there a way to enforce that a Rust raw pointer is not used after returning from a specific stack frame?

那年仲夏 提交于 2021-02-05 06:43:27
问题 I'm writing a Rust wrapper for a (mostly C-style) C++ plug-in SDK. The plug-in host is a graphical desktop application that runs an event loop. The plug-in is regularly called as part of that event loop. Whenever this happens, the plug-in has control and can call arbitrary host functions. One C function which I want to wrap returns a raw pointer. Right after that function returns, the pointer is guaranteed to be a valid C string, so it is safe to dereference it. However, after the plug-in

Spawning tasks with non-static lifetimes with tokio 0.1.x

纵然是瞬间 提交于 2021-02-04 07:08:50
问题 I have a tokio core whose main task is running a websocket (client). When I receive some messages from the server, I want to execute a new task that will update some data. Below is a minimal failing example: use tokio_core::reactor::{Core, Handle}; use futures::future::Future; use futures::future; struct Client { handle: Handle, data: usize, } impl Client { fn update_data(&mut self) { // spawn a new task that updates the data self.handle.spawn(future::ok(()).and_then(|x| { self.data += 1; //

Writing a generic function that takes an iterable container as parameter in Rust

笑着哭i 提交于 2021-02-02 07:34:01
问题 I want to write a generic function that takes any immutably borrowed iterable container such as an array, Vec , BTreeSet , etc. Since this function is part of a trait that I am implementing, I am not able to change the signature of it, so it's not possible to directly take an iterator as parameter and I also can't introduce any lifetime parameters to the function signature. Context I tried to implement the observer pattern in Rust. The observable and the observer look as follows: struct

Writing a generic function that takes an iterable container as parameter in Rust

喜你入骨 提交于 2021-02-02 07:32:35
问题 I want to write a generic function that takes any immutably borrowed iterable container such as an array, Vec , BTreeSet , etc. Since this function is part of a trait that I am implementing, I am not able to change the signature of it, so it's not possible to directly take an iterator as parameter and I also can't introduce any lifetime parameters to the function signature. Context I tried to implement the observer pattern in Rust. The observable and the observer look as follows: struct

Create closure returning iterator on string

青春壹個敷衍的年華 提交于 2021-01-29 10:57:35
问题 I want to write a closure that takes an object and returns an iterator from it. The idea is to store the closure in a structure and apply as needed: fn main() { let iter_wrap = |x: &String| Box::new(x.chars()); let test = String::from("test"); for x in iter_wrap(&test) { println!("{}", x); } } This causes the error: error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements --> src/main.rs:2:45 | 2 | let iter_wrap = |x: &String|

Rust FFI, callbacks, and lifetimes

≡放荡痞女 提交于 2021-01-29 08:34:02
问题 I'm trying to build a nice rust wrapper around libuv, an event loop library written in C. I'm pretty much "done", but I'm having some trouble with callbacks and lifetimes. Being an event loop library, libuv relies heavily on callbacks. I have some code that can accept functions, closures, or a tuple of (obj, method-on-obj) and it handles creating an appropriate "trampoline" to get that across the FFI boundary. That all works. However, the problem I'm running into is that I cannot figure out

How can I extend the lifetime of a temporary variable inside of an iterator adaptor in Rust?

雨燕双飞 提交于 2021-01-29 07:51:09
问题 I have a method make_iter() which creates an Iterator with multiple adapters in Rust, which can be simplified as the following MCVE: fn make_iter(first: &First) -> Box<dyn Iterator<Item = String> + '_> { Box::new(first.make_objects().flat_map(|second| { second .iter() .filter(|third| third.as_str() != "t2") .flat_map(|third| vec![format!("{}.A", third), format!("{}.B", third)].into_iter()) .chain( vec![ format!("{}.A", second.name()), format!("{}.B", second.name()), ] .into_iter(), ) })) }