lifetime

Sharing a struct with trait objects as properties across threads

点点圈 提交于 2019-12-17 14:28:49
问题 I have the code below. With the commented out parts, it's working. When I uncomment the parts it does not compile anymore. How can I adjust the commented parts to make them work, i.e., I want to make threads access the expression tree simultaneously. When I try it, the compiler starts with errors about thread safeness. I read the Rust book and know C/C++, but didn't understood everything about Rust type system and semantics yet. use std::thread; use std::sync::Arc; pub trait Expr { fn run(

Sharing a struct with trait objects as properties across threads

烈酒焚心 提交于 2019-12-17 14:28:24
问题 I have the code below. With the commented out parts, it's working. When I uncomment the parts it does not compile anymore. How can I adjust the commented parts to make them work, i.e., I want to make threads access the expression tree simultaneously. When I try it, the compiler starts with errors about thread safeness. I read the Rust book and know C/C++, but didn't understood everything about Rust type system and semantics yet. use std::thread; use std::sync::Arc; pub trait Expr { fn run(

Singleton Per Call Context (Web Request) in Unity

a 夏天 提交于 2019-12-17 08:11:07
问题 A few days ago, I had an issue with ASP.Net threading. I wanted to have a singleton object per web request. I actually need this for my unit of work. I wanted to instantiate a unit of work per web request so that identity map is valid through out the request. This way I could use an IoC to inject my own IUnitOfWork to my repository classes transparently, and I could use the same instance to query and then update my entities. Since I am using Unity, I mistakenly used PerThreadLifeTimeManager.

Why can't I return an &str value generated from a String?

感情迁移 提交于 2019-12-17 07:48:12
问题 I'm having some trouble trying to grasp why I can't return an &str value generated from a String (goodness, when will as_str be ready?) and I'm doing something wrong. I get this idea because nothing that I do makes the value live long enough to use. I'm trying to implement error::Error for a custom struct: impl error::Error for LexicalError { fn description(&self) -> &str { let s = format!("{}", self); // s doesn't live long enough to do this, I've tried // cloning s and using that, but still

Why can't I return an &str value generated from a String?

喜夏-厌秋 提交于 2019-12-17 07:48:10
问题 I'm having some trouble trying to grasp why I can't return an &str value generated from a String (goodness, when will as_str be ready?) and I'm doing something wrong. I get this idea because nothing that I do makes the value live long enough to use. I'm trying to implement error::Error for a custom struct: impl error::Error for LexicalError { fn description(&self) -> &str { let s = format!("{}", self); // s doesn't live long enough to do this, I've tried // cloning s and using that, but still

How to declare a lifetime for a closure argument?

落爺英雄遲暮 提交于 2019-12-17 07:39:50
问题 I would like to declare a lifetime for a closure in Rust, but I can't find a way to add a lifetime declaration. use std::str::SplitWhitespace; pub struct ParserError { pub message: String, } fn missing_token(line_no: usize) -> ParserError { ParserError { message: format!("Missing token on line {}", line_no), } } fn process_string(line: &str, line_number: usize) -> Result<(), ParserError> { let mut tokens = line.split_whitespace(); match try!(tokens.next().ok_or(missing_token(line_number))) {

Lifetime troubles sharing references between threads

柔情痞子 提交于 2019-12-17 03:19:00
问题 I've got a thread that launches worker threads, all are expected to live forever. Each worker thread maintains it's own list of Socket s. Some operations require that I traverse all sockets currently alive, but I'm having trouble with lifetimes trying to create a master list of sockets containing a pointer to a socket owned by another list. use std::{str, thread}; use std::thread::JoinHandle; use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; use std::sync::{Arc, Mutex}; use

“life-time” of string literal in C

瘦欲@ 提交于 2019-12-16 22:12:10
问题 Wouldn't the pointer returned by the following function inaccessible? char *foo( int rc ) { switch (rc) { case 1: return("one"); case 2: return("two"); default: return("whatever"); } } So the lifetime of a local variable in C/C++ is practically only within the function, right? Which means, after char* foo(int) terminates, the pointer it returns no longer means anything? I'm a bit confused about lifetime of local var. Could anyone give me a good clarification? 回答1: Yes, lifetime of an local

How do I set the return value's lifetime? [closed]

爷,独闯天下 提交于 2019-12-13 22:48:45
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 months ago . I have the following method to check if an String ID exists. If it doesn't, generate and then return it: fn generate_id(&self) -> ID<'m> { let id = nanoid::generate(15); while self[&id].is_some() { id = nanoid::generate(15); }; id } ID is a type alias: type ID<'id> = &'id String; The return value needs to be &

“Variable does not live long enough” when returning a Result containing a reference but it does live long enough

人走茶凉 提交于 2019-12-13 16:15:46
问题 I'm implementing a small utility and the compiler is telling me that a variable (a TcpStream ) does not live long enough and is advising me to find a way to make it live exactly as long as it is currently living. Error message error[E0597]: `stream` does not live long enough --> src/main.rs:47:35 | 47 | match handle_request(&mut stream){ | ^^^^^^ borrowed value does not live long enough ... 54 | } | - borrowed value only lives until here | note: borrowed value must be valid for the anonymous