lifetime

Questions about Rust lifetime

江枫思渺然 提交于 2019-12-11 14:55:13
问题 I'm trying to implement a memory pool based on TypedArena . Here's a simplified version of my original code: #![feature(rustc_private)] extern crate arena; use arena::TypedArena; pub struct MemoryPool { arena: TypedArena<Vec<u8>>, bytes_allocated: usize, } impl MemoryPool { pub fn consume(&mut self, buf: Vec<u8>) -> &[u8] { self.bytes_allocated += buf.capacity(); self.arena.alloc(buf) } } pub struct ByteArray<'a> { data: &'a [u8], } impl<'a> ByteArray<'a> { pub fn set_data(&mut self, data: &

error: `line` does not live long enough but it's ok in playground

孤街醉人 提交于 2019-12-11 13:44:07
问题 I can't figure it out why my local var line does not live long enough . You can see bellow my code. It work on the Rust's playground. I may have an idea of the issue: I use a structure (load is a function of this structure). As I want to store the result of the line in a member of my struct, it could be the issue. But I don't see what should I do to resolve this problem. pub struct Config<'a> { file: &'a str, params: HashMap<&'a str, &'a str> } impl<'a> Config<'a> { pub fn new(file: &str) ->

Passing a Vec<Struct> into a new task

巧了我就是萌 提交于 2019-12-11 11:37:12
问题 Im attempting to pass a Vector of a custom struct into a function that is executed in a new task. I've implemented the Clone trait, which I thought was needed for this, but apparently the vector I want to pass in needs to implement 'static+Send in order to be captured in the closure's environment. I'm not exactly sure how to go about satisfying those lifetime specs? I'm attempting to start the process from this function pub fn start(server: Server, ip: &str, port: u16) { // Other things

What is the point of an explicit lifetime for a method that doesn't take any arguments?

怎甘沉沦 提交于 2019-12-11 07:58:54
问题 On page 295 of Programming Rust you can find the following: Fortunately, the standard library includes the blanket implementation: impl<'a, T, U> AsRef<U> for &'a T where T: AsRef<U>, T: ?Sized, U: ?Sized, { fn as_ref(&self) -> &U { (*self).as_ref() } } I'm confused at the use of &'a there. What is the context of that? It's not being used in an argument of as_ref nor tied to the output of &U . I don't think I fully understand lifetimes when used in this context. I looked this up because I

Prolonging life of a temporary object using const reference

流过昼夜 提交于 2019-12-11 06:15:35
问题 I need a few clarification regarding const reference. From this link: const Foo &myFoo = FuncBar(); const reference extended the life of the local object. But when I checked this link although they used a const reference Sandbox(const string& n) : member(n) {} the lifetime of string "four" did not increase. Sandbox sandbox(string("four")); They used the sentence Only local const references prolong the lifespan. Then in the second link isn't the string "four" local to the main function and

How to use the lifetime on AsRef

独自空忆成欢 提交于 2019-12-11 05:43:35
问题 I'm having a hard time understanding how to use lifetimes with the code below. I understand that explicit lifetimes are necessary to aid the compiler in understanding when it can hold/release data but in this particular case, url.serialize() generates an anonymous string and I'm not really sure how to resolve this issue. impl AsRef<str> for RequestUri { #[inline] fn as_ref(&self) -> &str { match self { &RequestUri::AbsoluteUri(ref url) => url.serialize().as_ref() } } } 回答1: The docs for AsRef

Cannot infer a lifetime for a struct containing a reference to a closure [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-11 05:33:14
问题 This question already has answers here : Lifetime annotation for closure argument (2 answers) How to declare a lifetime for a closure argument? (3 answers) Closed 2 years ago . I am trying to make this simplified and self-contained version of my code compile: struct FragMsgReceiver<'a, 'b: 'a> { recv_dgram: &'a mut FnMut(&mut [u8]) -> Result<&'b mut [u8], ()>, } impl<'a, 'b> FragMsgReceiver<'a, 'b> { fn new( recv_dgram: &'a mut FnMut(&mut [u8]) -> Result<&'b mut [u8], ()> ) -> Self {

How to use mutable member Vec?

人走茶凉 提交于 2019-12-11 02:07:50
问题 How to properly create a member Vec ? What am I missing here? struct PG { names: &mut Vec<String>, } impl PG { fn new() -> PG { PG { names: Vec::new() } } fn push(&self, s: String) { self.names.push(s); } } fn main() { let pg = PG::new(); pg.push("John".to_string()); } If I compile this code, I get: error[E0106]: missing lifetime specifier --> src/main.rs:2:12 | 2 | names: &mut Vec<String>, | ^ expected lifetime parameter If I change the type of names to &'static mut Vec<String> , I get:

DbContext Unity does not call HttpContextLifetimeManager.RemoveValue() Bad thing?

馋奶兔 提交于 2019-12-11 01:03:29
问题 I'm defining my DbConntextObj _container.RegisterType<IDbConntextObj, DbConntextObj>(new HttpContextLifetimeManager<DbConntextObj>()); Unity is not calling the RemoveValue() on the lifetimemanager I have one Dbcontext for multiple repositories. My lifetimemanager looks like this: public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable { private readonly string _itemName = typeof(T).AssemblyQualifiedName; public override object GetValue() { return HttpContext.Current.Items[

Am I incorrectly implementing IntoIterator for a reference to a LazyList implementation or is this a Rust bug?

泪湿孤枕 提交于 2019-12-10 19:55:35
问题 In implementing a version of a LazyList (an immutable lazily-computed memoized singly-linked list, much as Haskell lists), I have run into a problem of implementing IntoIterator in that the code does not drop the reference when I think it should. The following code has been simplified so as just to show the problem; thus, is not generic and does not include all of the methods not related to implementing IntoIterator : use std::cell::UnsafeCell; use std::mem::replace; use std::rc::Rc; // only