rust

How to accept an async function as an argument?

倖福魔咒の 提交于 2020-07-21 11:13:58
问题 I would like to replicate the behavior and ergonomics of taking a closure/function as an argument much like map does: iterator.map(|x| ...) . I've noticed that some library code allows passing in async functionality, but this method doesn't allow me to pass in arguments: pub fn spawn<F, T>(future: F) -> JoinHandle<T> where F: Future<Output = T> + Send + 'static, T: Send + 'static, spawn(async { foo().await }); I'm hoping to do one of the following: iterator.map(async |x| {...}); async fn a(x:

How to accept an async function as an argument?

陌路散爱 提交于 2020-07-21 11:13:34
问题 I would like to replicate the behavior and ergonomics of taking a closure/function as an argument much like map does: iterator.map(|x| ...) . I've noticed that some library code allows passing in async functionality, but this method doesn't allow me to pass in arguments: pub fn spawn<F, T>(future: F) -> JoinHandle<T> where F: Future<Output = T> + Send + 'static, T: Send + 'static, spawn(async { foo().await }); I'm hoping to do one of the following: iterator.map(async |x| {...}); async fn a(x:

How to accept an async function as an argument?

六眼飞鱼酱① 提交于 2020-07-21 11:13:01
问题 I would like to replicate the behavior and ergonomics of taking a closure/function as an argument much like map does: iterator.map(|x| ...) . I've noticed that some library code allows passing in async functionality, but this method doesn't allow me to pass in arguments: pub fn spawn<F, T>(future: F) -> JoinHandle<T> where F: Future<Output = T> + Send + 'static, T: Send + 'static, spawn(async { foo().await }); I'm hoping to do one of the following: iterator.map(async |x| {...}); async fn a(x:

Is this the most natural way to read structs from a binary file?

怎甘沉沦 提交于 2020-07-21 04:21:11
问题 I just realized this is quite similar to What is the best way to parse binary protocols with Rust Is this the most natural way to read structs from a binary file using Rust? It works but seems a bit odd (why can't I just fill the struct wholesale?). extern crate byteorder; use byteorder::{ByteOrder, LittleEndian}; struct Record { latch: u32, total_energy: f32, x_cm: f32, y_cm: f32, x_cos: f32, y_cos: f32, weight: f32 } impl Record { fn make_from_bytes(buffer: &[u8]) -> Record { Record { latch

How can one await a result of a boxed future?

蓝咒 提交于 2020-07-21 02:26:25
问题 use futures::{future, Future}; fn test() -> Box<dyn Future<Output = bool>> { Box::new(future::ok::<bool>(true)) } async fn async_fn() -> bool { let result: bool = test().await; return result; } fn main(){ async_fn(); println!("Hello!"); } Playground Error: error[E0277]: the trait bound `dyn core::future::future::Future<Output = bool>: std::marker::Unpin` is not satisfied --> src/main.rs:8:24 | 8 | let result: bool = test().await; | ^^^^^^^^^^^^ the trait `std::marker::Unpin` is not

How to properly wrap a C function pointer in Rust? [duplicate]

自闭症网瘾萝莉.ら 提交于 2020-07-19 05:58:09
问题 This question already has answers here : Why does the address of an object change across methods? (2 answers) Closed 4 months ago . I have a C struct Foo with a function pointer. In my Rust bindings, I would like to allow users to set this function pointer, but I would like to avoid users having to deal with FFI types. foo.h struct Foo { void* internal; uint8_t a; void (*cb_mutate_a)(void*); }; struct Foo* foo_new(); void foo_free(struct Foo* foo); void foo_call(struct Foo* foo); foo.c struct

How to properly wrap a C function pointer in Rust? [duplicate]

走远了吗. 提交于 2020-07-19 05:58:08
问题 This question already has answers here : Why does the address of an object change across methods? (2 answers) Closed 4 months ago . I have a C struct Foo with a function pointer. In my Rust bindings, I would like to allow users to set this function pointer, but I would like to avoid users having to deal with FFI types. foo.h struct Foo { void* internal; uint8_t a; void (*cb_mutate_a)(void*); }; struct Foo* foo_new(); void foo_free(struct Foo* foo); void foo_call(struct Foo* foo); foo.c struct

How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it?

耗尽温柔 提交于 2020-07-19 04:07:49
问题 I have a struct with a lifetime: struct HasLifetime<'a>( /* ... */ ); There is there is an implementation of the trait Foo : impl<'a, 'b: 'a> Foo for &'a mut HasLifetime<'b> { } I want to implement the following function: fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut Lifetime<'b>) -> impl Foo { bar } This won't compile because the returned impl is only valid for 'a . However, specifying impl Foo + 'a results in: error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in

How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it?

↘锁芯ラ 提交于 2020-07-19 04:04:00
问题 I have a struct with a lifetime: struct HasLifetime<'a>( /* ... */ ); There is there is an implementation of the trait Foo : impl<'a, 'b: 'a> Foo for &'a mut HasLifetime<'b> { } I want to implement the following function: fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut Lifetime<'b>) -> impl Foo { bar } This won't compile because the returned impl is only valid for 'a . However, specifying impl Foo + 'a results in: error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in

How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it?

廉价感情. 提交于 2020-07-19 04:03:08
问题 I have a struct with a lifetime: struct HasLifetime<'a>( /* ... */ ); There is there is an implementation of the trait Foo : impl<'a, 'b: 'a> Foo for &'a mut HasLifetime<'b> { } I want to implement the following function: fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut Lifetime<'b>) -> impl Foo { bar } This won't compile because the returned impl is only valid for 'a . However, specifying impl Foo + 'a results in: error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in