rust

Deserialize a Vec<Foobar<T>> as Vec<T> directly when Foobar has exactly one field

不打扰是莪最后的温柔 提交于 2020-01-06 07:01:43
问题 I'm given a data-format that includes a sequence of objects with exactly one named field value each. Can I remove this layer of indirection while deserializing? When deserializing, the natural representation would be /// Each record has it's own `{ value: ... }` object #[derive(serde::Deserialize)] struct Foobar<T> { value: T, } /// The naive representation, via `Foobar`... #[derive(serde::Deserialize)] struct FoobarContainer { values: Vec<Foobar<T>>, } While Foobar adds no extra cost beyond

Deserialize a Vec<Foobar<T>> as Vec<T> directly when Foobar has exactly one field

杀马特。学长 韩版系。学妹 提交于 2020-01-06 07:01:14
问题 I'm given a data-format that includes a sequence of objects with exactly one named field value each. Can I remove this layer of indirection while deserializing? When deserializing, the natural representation would be /// Each record has it's own `{ value: ... }` object #[derive(serde::Deserialize)] struct Foobar<T> { value: T, } /// The naive representation, via `Foobar`... #[derive(serde::Deserialize)] struct FoobarContainer { values: Vec<Foobar<T>>, } While Foobar adds no extra cost beyond

Pass a Struct<'a>::method as a `for<'a> Fn(&Foo<'a>)` for use in a closure

怎甘沉沦 提交于 2020-01-06 06:53:07
问题 In my tests I had a helper function that runs a given method on differently configured objects, with a simplified version looking like this: fn run_method<F>(f: F) where F: Fn(&Foo), { let to_test = vec![0i32]; to_test .iter() .map(|param| { let foo = Foo(*param); f(&foo); }) .for_each(drop); } // run_method(Foo::run); This worked fine until I added references to the tested struct, making it "lifetime-annotated" (for lack of a proper term, I mean Foo<'a> ). Now I get an error indicating, I

How do you convert Vec<&mut T> to Vec<&T>?

こ雲淡風輕ζ 提交于 2020-01-06 06:48:08
问题 I've got a vector of mutable references: type T = String; let mut mut_vec: Vec<&mut T> = vec![]; I want to pass (a copy of) it into a function that takes a vector of immutable references: fn cool_func(mut immut_vec: Vec<&T>) -> bool {false} How can I do this? 回答1: You can dereference and reborrow the mutable references, then add them to a new Vec : fn main() { let mut st = String::new(); let mut_vec = vec![&mut st]; let immut_vec = mut_vec.into_iter().map(|x| &*x).collect(); cool_func(immut

How to link a dynamic Rust library using only rustc and not cargo?

廉价感情. 提交于 2020-01-06 06:12:31
问题 My main.rs looks like // #[link(name = "lib")] extern "C" { fn hello(); } fn main() { unsafe { hello(); } } And lib.rs : #[no_mangle] pub fn hello() { println!("Hello, World!"); } I have compiled lib.rs using rustc --crate-type=cdylib lib.rs -o lib.so How do I link lib.so to rustc main.rs command ? 回答1: You need to match ABIs. When you use an extern "C" block, you need to declare your functions using the same ABI. Name your dynamic library using the platform's conventions. Use .dylib on macOS

How to link a dynamic Rust library using only rustc and not cargo?

孤者浪人 提交于 2020-01-06 06:11:48
问题 My main.rs looks like // #[link(name = "lib")] extern "C" { fn hello(); } fn main() { unsafe { hello(); } } And lib.rs : #[no_mangle] pub fn hello() { println!("Hello, World!"); } I have compiled lib.rs using rustc --crate-type=cdylib lib.rs -o lib.so How do I link lib.so to rustc main.rs command ? 回答1: You need to match ABIs. When you use an extern "C" block, you need to declare your functions using the same ABI. Name your dynamic library using the platform's conventions. Use .dylib on macOS

Is there a way to launch a tokio::Delay on a new thread to allow the main loop to continue?

霸气de小男生 提交于 2020-01-06 05:50:10
问题 I am trying to run a function at the end of a delay if the timer is not canceled. The use case is a press and hold / double tap for user input. The main issue that I am having is that the tokio::run(task); stops the main loop from executing thus prevents me from evaluating the state of the users control. fn start_timer(&self) { let function_name = self.combo.function_name.clone(); let when = Instant::now() + Duration::from_millis(self.combo.timer_value as u64); let task = Delay::new(when)

Cargo test cannot reference anything public inside the targeting crate in integration test. Unit tests also cannot find test cases

喜欢而已 提交于 2020-01-06 05:48:06
问题 I am trying to do some test on my no_std module but I cannot make both integration and unit test working. I think the cargo does not make functions and modules visible to others. The project is at: https://github.com/ShisoftResearch/Nulloc/tree/e2e15646da79651ddb8c29e0526bad0d60690bec Run cargo test , I got: ➜ cargo test Compiling nulloc v0.1.0 (/Users/shisoft/Dropbox/Code/OSS Projects/Nulloc) error[E0432]: unresolved import `nulloc::bump_heap` --> tests/bump_heap.rs:3:14 | 3 | use nulloc:

How do I erase the type of future in the new future API?

扶醉桌前 提交于 2020-01-06 05:15:17
问题 The following does not compile #![feature(await_macro, async_await, futures_api)] use core::future::Future; async fn foo() {} trait Bar { type Output: Future<Output = ()>; fn bar(&self) -> Self::Output; } impl Bar for () { type Output = Box<dyn Future<Output = ()>>; fn bar(&self) -> Self::Output { Box::new(foo()) } } async fn buz() { await!(().bar()) } error[E0277]: the trait bound `(dyn std::future::Future<Output=()> + 'static): std::marker::Unpin` is not satisfied --> src/lib.rs:19:15 | 19

Why can't I store a value and a reference to that value in the same struct?

℡╲_俬逩灬. 提交于 2020-01-06 05:07:26
问题 I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<'a>(Thing, &'a u32); fn make_combined<'a>() -> Combined<'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } Sometimes, I have a value and I want to store that value and a reference to that value in the same structure: struct Combined<'a>(Thing, &'a Thing); fn make_combined<'a>() -> Combined<'a> { let thing = Thing::new();