Created
September 23, 2015 21:14
-
-
Save bbatha/85c1683115e40838ec35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate scoped_threadpool; | |
use scoped_threadpool::{Pool, Scope}; | |
#[derive(Debug)] | |
pub struct RefOwner<'a>(&'a str); | |
pub struct ScopeRef<'pool, 'scope> where 'pool: 'scope { | |
scope: &'scope Scope<'pool, 'scope>, | |
} | |
impl<'pool, 'scope> ScopeRef<'pool, 'scope> { | |
pub fn new(scope: &'scope Scope<'pool, 'scope>) -> ScopeRef<'pool, 'scope> { | |
ScopeRef { scope: scope } | |
} | |
pub fn execute(&self, ref_owner: &'scope RefOwner) -> String { | |
self.scope.execute(move || { | |
println!("Got: {:?}", ref_owner); | |
}); | |
ref_owner.0.to_owned() | |
} | |
} | |
#[test] | |
fn it_works() { | |
let foo = "foo".to_owned(); | |
let mut pool = Pool::new(2); | |
pool.scoped(|scope| { | |
let scope_ref = ScopeRef::new(scope); | |
let result = scope_ref.execute(&RefOwner(&foo)); | |
assert_eq!(foo, result); | |
}); | |
} | |
src/lib.rs:35:43: 35:48 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements [E0495] | |
src/lib.rs:35 let scope_ref = ScopeRef { scope: scope }; | |
^~~~~ | |
src/lib.rs:35:43: 35:48 note: first, the lifetime cannot outlive the expression at 35:42... | |
src/lib.rs:35 let scope_ref = ScopeRef { scope: scope }; | |
^~~~~ | |
src/lib.rs:35:43: 35:48 note: ...so that auto-reference is valid at the time of borrow | |
src/lib.rs:35 let scope_ref = ScopeRef { scope: scope }; | |
^~~~~ | |
src/lib.rs:31:25: 40:6 note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the block at 31:24... | |
src/lib.rs:31 pool.scoped(|scope| { | |
src/lib.rs:32 /* | |
src/lib.rs:33 let scope_ref = ScopeRef::new(scope); | |
src/lib.rs:34 */ | |
src/lib.rs:35 let scope_ref = ScopeRef { scope: scope }; | |
src/lib.rs:36 /* | |
... | |
src/lib.rs:31:23: 31:24 note: ...so that the reference type `&scoped_threadpool::Scope<'_, '_>` does not outlive the data it points at | |
src/lib.rs:31 pool.scoped(|scope| { | |
^ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment