rust

How to force a union to behave as if there is only one type?

我与影子孤独终老i 提交于 2021-01-27 16:12:36
问题 I'm trying to write a wrapper for the epoll Linux API. I forked this repository, but this crate doesn't use the union type used by the epoll API. I decided to use Rust's C union feature to create a complete wrapper where user will not need to use unsafe code. This union causes me some trouble. How could I lock the used type of the union to one type at compile time? epoll's union can't be differentiated; you can only use one member of the union by epoll fd. Well, you can but that will not be

How to force a union to behave as if there is only one type?

依然范特西╮ 提交于 2021-01-27 16:10:02
问题 I'm trying to write a wrapper for the epoll Linux API. I forked this repository, but this crate doesn't use the union type used by the epoll API. I decided to use Rust's C union feature to create a complete wrapper where user will not need to use unsafe code. This union causes me some trouble. How could I lock the used type of the union to one type at compile time? epoll's union can't be differentiated; you can only use one member of the union by epoll fd. Well, you can but that will not be

How to force a union to behave as if there is only one type?

狂风中的少年 提交于 2021-01-27 15:41:28
问题 I'm trying to write a wrapper for the epoll Linux API. I forked this repository, but this crate doesn't use the union type used by the epoll API. I decided to use Rust's C union feature to create a complete wrapper where user will not need to use unsafe code. This union causes me some trouble. How could I lock the used type of the union to one type at compile time? epoll's union can't be differentiated; you can only use one member of the union by epoll fd. Well, you can but that will not be

Specifying associated type in trait that inherits from another trait

不问归期 提交于 2021-01-27 15:01:37
问题 I started working on my first more ambitious Rust project, and struggle with something I did not come across in any of the resources and tutorials I used for learning. The title of the question captures the abstract problem, but for the examples I'll use the concrete examples I am fighting with. For my project, I need to interface with different third-party services, and I decided to use the actix framework as an abstraction for the different actors in my domain. The framework defines the

How can I use a custom hash function in a HashSet or HashMap?

本小妞迷上赌 提交于 2021-01-27 14:49:07
问题 Since SipHasher is too slow for my use case, I'm trying to implement a custom hash function. I found an example which I used as base to get everything compiling. My current code looks like this: use std::collections::hash_state::{DefaultState}; use std::collections::{HashMap, HashSet}; use std::default::Default; use std::hash::{Hash, Hasher, SipHasher}; use std::marker; pub struct FnvHasher(u64); impl Default for FnvHasher { fn default() -> FnvHasher { FnvHasher(0xcbf29ce484222325) } } impl

How to generate tuples from strings?

China☆狼群 提交于 2021-01-27 14:41:54
问题 I am writing a macro to parse some structured text into tuples, line by line. Most parts work now, but I am stuck at forming a tuple by extracting/converting Strings from a vector. // Reading Tuple from a line // Example : read_tuple( "1 ab 3".lines() // ,(i32, String, i32)) // Expected : (1, "ab", 3) // Note:: you can note use str macro_rules! read_tuple { ( $lines :ident , ( $( $t :ty ),* ) ) => {{ let l = ($lines).next().unwrap(); let ws = l.trim().split(" ").collect::<Vec<_>>(); let s : (

How does Rust handle shadowed variables?

只愿长相守 提交于 2021-01-27 13:50:00
问题 I have strong C/C++ background and am learning Rust these days. Got puzzled by how Rust handles shadowing variables. Particularly, I was expecting that the following code segment shall run without problem because guess is shadowed from a String to an integer before the next time it is called as a String in read_line . Reading the API document, I understand read_line would append the next input to guess . But after the shadowing, should guess be considered as an integer and such appending be

Does Rust contain a way to directly check whether or not one vector is a “substring” of another?

岁酱吖の 提交于 2021-01-27 13:26:42
问题 You can do this with a String using contains which searches for a pattern, but Vec::contains is for a single element. The only way I've been able to do this is by directly implementing some kind of substring function, but I'm sort of hoping there's a built-in way. let vec1 = vec![1, 2, 3, 4, 5]; let vec2 = vec![2, 3]; // vec2 IS a substring of vec1 let vec3 = vec![1, 5]; // vec3 is NOT a substring of vec3 fn is_subvec(mainvec: &Vec<i32>, subvec: &Vec<i32>) -> bool { if subvec.len() == 0 {

How to get ownership of a moved value back from a closure?

本秂侑毒 提交于 2021-01-27 13:21:24
问题 Consider the following program: fn primes_up_to(n: usize) -> Vec<usize> { let mut ans = Vec::with_capacity(n); if n < 2 { return ans; } ans.push(2); // https://doc.rust-lang.org/1.22.0/book/first-edition/closures.html#closures-and-their-environment // The closure needs ownership of vec to access elements let is_prime = |n: usize| -> bool { for x in ans { if x * x > n { break; } if n % x == 0 { return false; } } true }; let mut i = 3; while i <= n { if is_prime(i) { ans.push(i); } i += 2; }

Writing a chunk stream to a file asynchronously using hyper

拥有回忆 提交于 2021-01-27 13:01:48
问题 I am trying to create a simple function that downloads a remote file to a local filepath using hyper. I need the file write to be asynchronous as well (in my case I am using tokio_fs for that). Here is the code: View in the playground // Parts of the code were omitted, see the playground for full source code pub fn download_file( uri: Uri, file_location: &Path, ) -> Box<Future<Item = (), Error = DownloadFileError>> { let temp_dir_path = tempfile::tempdir().unwrap().into_path(); let file_name