lifetime

Converting from String to &str with a different lifetime

╄→尐↘猪︶ㄣ 提交于 2019-12-12 04:56:39
问题 I have this simple example: fn make_string<'a>() -> &'a str { let s : &'static str = "test"; s } fn make_str<'a>() -> &'a str { let s : String = String::from_str("test"); s.as_slice() } fn main() { println!("{}", make_string()); println!("{}", make_str()); } Error message: <anon>:8:9: 8:10 error: `s` does not live long enough <anon>:8 s.as_slice() ^ <anon>:6:34: 9:6 note: reference must be valid for the lifetime 'a as defined on the block at 6:33... <anon>:6 fn make_str<'a>() -> &'a str {

Cannot infer a lifetime for a closure returning a boxed trait that contains a reference

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 04:38:33
问题 I am trying to compile the following code (playground): trait MockFutureTrait { type Item; } struct MockFuture<T> { item: T, } impl<T> MockFutureTrait for MockFuture<T> { type Item = T; } struct FragMsgReceiver<'a, 'c: 'a> { recv_dgram: &'a FnMut(&mut [u8]) -> Box<MockFutureTrait<Item = &mut [u8]> + 'c>, } fn constrain_handler<F>(f: F) -> F where F: FnMut(&mut [u8]) -> Box<MockFutureTrait<Item = &mut [u8]>>, { f } fn main() { let mut recv_dgram = constrain_handler(|buf: &mut [u8]| { Box::new

How can I have a lifetime dependency without a reference?

谁说我不能喝 提交于 2019-12-12 04:16:20
问题 I'm wrapping a C library that has context and device objects. The context object needs to outlive the device object because the device keeps an internal reference to the context . To express this, I use a PhantomData field in the Device wrapper: use std::marker::PhantomData; struct Context; impl Context { fn open_device<'a>(&'a self) -> Device<'a> { Device { _context: PhantomData, } } } struct Device<'a> { _context: PhantomData<&'a Context>, } Now, in my client code, I would like to have a

Enforce SqlConnection to be open for specified duration

与世无争的帅哥 提交于 2019-12-12 03:39:45
问题 It seems that SqlConnections close after some period of time of inactivity. I need to be able to control for how long the connection remains open. There is a "connection timeout" and "Connection Lifetime" properties that can be configured for connections, but they do not do what I need. The problem is that our application is huge and in lots of places connections don't get closed when some screens are open. A user can open a screen, then go to lunch, then come back, then try to do something

Lifetime of variable in a match pattern

只谈情不闲聊 提交于 2019-12-12 03:15:31
问题 Trying to compile the following code: #[derive(Show)] pub enum E1 { A, B, } #[derive(Show)] pub enum E2 { X(E1), Y(i32), } impl std::fmt::String for E1 { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { std::fmt::Show::fmt(self, f) } } impl std::fmt::String for E2 { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { std::fmt::Show::fmt(self, f) } } impl std::error::Error for E2 { fn description(&self) -> &'static str { match *self { E2::X(x) => { let d: &'static

Sorting out different lifetimes on Self and a method

╄→尐↘猪︶ㄣ 提交于 2019-12-12 01:58:47
问题 I posted a similar question (Rust lifetime error expected concrete lifetime but found bound lifetime) last night, but still can't figure out how to apply it to this case now. Once again, a simplified example bellow: struct Ref; struct Container<'a> { r : &'a Ref } struct ContainerB<'a> { c : Container<'a> } trait ToC { fn from_c<'a>(r : &'a Ref, c : Container<'a>) -> Self; } impl<'b> ToC for ContainerB<'b> { fn from_c<'a>(r : &'a Ref, c : Container<'a>) -> ContainerB<'a> { ContainerB{c:c} } }

How can I specify a lifetime for closure arguments?

…衆ロ難τιáo~ 提交于 2019-12-12 01:52:06
问题 Playpen link: http://is.gd/EpX6lM I have a closure that takes a slice and returns a subslice of it. Compiling the following code on rust-1.0.0-beta-2 fails: trait OptionalFirst { fn optional_first<'a>(&self, x: &'a [usize]) -> &'a [usize]; } impl<F> OptionalFirst for F where F: Fn(&[usize]) -> &[usize] { fn optional_first<'a>(&self, x: &'a [usize]) -> &'a [usize] { (*self)(x) } } fn main() { let bc: Box<OptionalFirst> = Box::new( |x: &[usize]| -> &[usize] { if x.len() != 0 { &x[..1] } else {

Storing variable value for lifetime in .Net?

与世无争的帅哥 提交于 2019-12-11 20:33:18
问题 I want to store a variable value that store its value life time. I know the following things that can be used but it may have a storing limits. Session Cookie Query string But I want to store value for lifetime. I heard about "application variables", but I don't know about their functionality. So, could anyone tell me about it and also suggest me is there any other way to store values? 回答1: Application is a key value dictionary of objects. Application state is stored in memory on the server

Closure as function parameter “cannot infer an appropriate lifetime due to conflicting requirements”

╄→гoц情女王★ 提交于 2019-12-11 19:26:36
问题 I am trying to use a closure as function parameter: fn foo(f: Box<Fn() -> bool>) -> bool { f() } fn main() { let bar = 42; foo(Box::new(|| bar != 42)); } but I get this lifetime error: src/main.rs:7:24: 7:36 error: cannot infer an appropriate lifetime due to conflicting requirements src/main.rs:7 let n = foo(Box::new(|| bar != 42)); ^~~~~~~~~~~~ src/main.rs:7:15: 7:23 note: first, the lifetime cannot outlive the expression at 7:14... src/main.rs:7 let n = foo(Box::new(|| bar != 42)); ^~~~~~~~

What is the better way to wrap a FFI struct that owns or borrows data?

心不动则不痛 提交于 2019-12-11 17:10:27
问题 I have an Image struct that can be constructed from a Vec<u8> or a &[u8] . It represents an image object in C library (ffi module). struct Image { ptr: *mut c_void }; impl Image { fn from_vec(vec: Vec<u8>) -> Image { // transfer ownership to gobject system let ptr = unsafe { ffi::new( vec.as_ptr() as *const c_void, vec.len(), .. ) }; std::mem::forget(vec); Image { ptr } } fn from_ref(data: &[u8]) -> Image { // gobject doesn't free data on Drop let ptr = unsafe { ffi::new_ref( data.as_ptr() as