rust

How to “unlock” an RwLock?

会有一股神秘感。 提交于 2020-06-13 04:53:08
问题 I'm trying to solve the thread-ring problem. In each thread I read the token value if it is not mine, check if it's the end of the program if it is then finish the thread otherwise, read again and repeat if it is mine (i.e. has my id) then acquire the write lock, increase the value of the token, check if it's the end then tell main thread that I finished it and finish the current thread loop If it not over, then release the write lock, and start to read again There is no unlock. Is there any

How to “unlock” an RwLock?

落花浮王杯 提交于 2020-06-13 04:50:08
问题 I'm trying to solve the thread-ring problem. In each thread I read the token value if it is not mine, check if it's the end of the program if it is then finish the thread otherwise, read again and repeat if it is mine (i.e. has my id) then acquire the write lock, increase the value of the token, check if it's the end then tell main thread that I finished it and finish the current thread loop If it not over, then release the write lock, and start to read again There is no unlock. Is there any

Does Iterator::collect allocate the same amount of memory as String::with_capacity?

ぐ巨炮叔叔 提交于 2020-06-12 06:47:11
问题 In C++ when joining a bunch of strings (where each element's size is known roughly), it's common to pre-allocate memory to avoid multiple re-allocations and moves: std::vector<std::string> words; constexpr size_t APPROX_SIZE = 20; std::string phrase; phrase.reserve((words.size() + 5) * APPROX_SIZE); // <-- avoid multiple allocations for (const auto &w : words) phrase.append(w); Similarly, I did this in Rust (this chunk needs the unicode-segmentation crate) fn reverse(input: &str) -> String {

Most idiomatic way to create a default struct

半城伤御伤魂 提交于 2020-06-11 18:09:58
问题 To create a default struct, I used to see fn new() -> Self in Rust, but today, I discovered Default . So there are two ways to create a default struct: struct Point { x: i32, y: i32, } impl Point { fn new() -> Self { Point { x: 0, y: 0, } } } impl Default for Point { fn default() -> Self { Point { x: 0, y: 0, } } } fn main() { let _p1 = Point::new(); let _p2: Point = Default::default(); } What is the better / the most idiomatic way to do so? 回答1: If you had to pick one, implementing the

Store a collection of heterogeneous types with generic type parameters in Rust

余生颓废 提交于 2020-06-11 06:52:49
问题 I'm trying to implement a basic ECS in Rust. I want a data structure storing, for each component, a storage of that particular component. Because some components are common while others are rare, I want different types of storage policies such as VecStorage<T> and HashMapStorage<T> . As components are unknown to the game engine's ECS, I came up with: trait AnyStorage: Debug { fn new() -> Self where Self: Sized; } #[derive(Default, Debug)] struct StorageMgr { storages: HashMap<TypeId, Box

Creating a simple Rust daemon that listens to a port

大憨熊 提交于 2020-06-10 18:06:51
问题 I've been trying to make a simple daemon in Rust that will listen to a port using tcp_stream and print the message. However, I'm running into two problems: 1) If my daemon uses println!, it crashes. If I remove all mentions of println!, the daemon works. How does stdout/stdin work when making a daemon? One source I found on the Rust mailing list says "With modern init systems, such as systemd or launchctl, this works very nicely and application developer doesn't have to care about

Creating a simple Rust daemon that listens to a port

孤街醉人 提交于 2020-06-10 18:06:19
问题 I've been trying to make a simple daemon in Rust that will listen to a port using tcp_stream and print the message. However, I'm running into two problems: 1) If my daemon uses println!, it crashes. If I remove all mentions of println!, the daemon works. How does stdout/stdin work when making a daemon? One source I found on the Rust mailing list says "With modern init systems, such as systemd or launchctl, this works very nicely and application developer doesn't have to care about

How can I free memory allocated by Rust code exposed in WebAssembly?

自作多情 提交于 2020-06-10 08:04:30
问题 I have a web application written in Rust and wasm-bindgen that needs to store state. The state is stored like this: lazy_static! { static ref ID_TO_DATA: Mutex<HashMap<u32, Data>> = Mutex::new(HashMap::new()); } pub struct Data { pub coder_id: u16, pub bools: Vec<bool>, pub ints: Vec<i32>, pub strings: Vec<String>, } I attempted the following to remove the data and free the memory, and the data is removed from the HashMap and no errors are reported: #[wasm_bindgen] pub fn remove_data(id: u32)

Is there a faster/shorter way to initialize variables in a Rust struct?

烂漫一生 提交于 2020-06-09 07:40:17
问题 In the following example, I would much prefer to assign a value to each field in the struct in the declaration of the fields. Alternatively, it effectively takes one additional statement for each field to assign a value to the fields. All I want to be able to do is to assign default values when the struct is instantiated. Is there a more succinct way of doing this? struct cParams { iInsertMax: i64, iUpdateMax: i64, iDeleteMax: i64, iInstanceMax: i64, tFirstInstance: bool, tCreateTables: bool,

How can I deserialize an enum when the case doesn't match?

ぐ巨炮叔叔 提交于 2020-06-08 19:20:25
问题 I have a JSON structure that looks like this: { "type": "suite", "event": "started", "test_count": 1 } I want to deserialize into these structs: #[derive(Debug, Deserialize)] enum ResultType { Suite, Test, } #[derive(Debug, Deserialize)] enum ResultEvent { Started, Failed, Ok, } #[derive(Debug, Deserialize)] struct JsonResult { #[serde(rename(deserialize = "type"))] test_type: ResultType, event: ResultEvent, test_count: Option<u32>, } I can't find a way to make serde_json use the correct case