future

How can I store an async function in a struct and call it from a struct instance?

£可爱£侵袭症+ 提交于 2020-06-07 04:41:09
问题 I'm trying to achieve this with the new async / await syntax, std::future::Future s and a recent version of Tokio. I'm using Tokio 0.2.0-alpha.4 and Rust 1.39.0-nightly . Different things I've tried include: using Box<dyn> s for the types that I want to store in the struct using generics in the struct definition I couldn't quite get a minimal working version, so here's a simplified version of what I'm trying to achieve: async fn foo(x: u8) -> u8 { 2 * x } // type StorableAsyncFn = Fn(u8) ->

Is there a converter from List<CompletionStage> to CompletionStage<List> in Java?

白昼怎懂夜的黑 提交于 2020-06-01 03:49:46
问题 Like in this hypothetical example of using of : List<CompletionStage<Long>> listOfFutureLongs = getFutureLongs(...) CompletionStage<List<Long>> futureListOfLongs = CompletionStage.of(listOfFutureLongs) 回答1: Strangely no. There's CompletableFuture.allOf for CompletableFuture , which is kind of like what you want, but no similar function for CompletionStage . You can either use CompletionStage.toCompletableFuture to get futures, or you can write your own. Unfortunately, the inability to check a

Is there a more ergonomic syntax for Either when using futures?

穿精又带淫゛_ 提交于 2020-06-01 02:28:53
问题 Here's an example of using Tokio to run a function that returns a future: use futures::sync::oneshot; use futures::Future; use std::thread; use std::time::Duration; use tokio; #[derive(Debug)] struct MyError { error_code: i32, } impl From<oneshot::Canceled> for MyError { fn from(_: oneshot::Canceled) -> MyError { MyError { error_code: 1 } } } fn deferred_task() -> impl Future<Item = i32, Error = MyError> { let (sx, rx) = oneshot::channel(); thread::spawn(move || { thread::sleep(Duration::from

Dart 2: Difference between Future<void> and Future<Null>

我是研究僧i 提交于 2020-05-25 06:18:46
问题 Having an asynchronous function that doesn't return a value, what's the ideal return type Future<Null> or Future<void> ?, or more specifically, what's the difference in using either? Both are legal, and in both cases the return value of the function is a Future that resolves to null . The following code prints null two times: import 'dart:async'; Future<void> someAsync() async {} Future<Null> otherAsync() async {} main() { someAsync().then((v) => print(v)); otherAsync().then((v) => print(v));

Dart 2: Difference between Future<void> and Future<Null>

折月煮酒 提交于 2020-05-25 06:15:30
问题 Having an asynchronous function that doesn't return a value, what's the ideal return type Future<Null> or Future<void> ?, or more specifically, what's the difference in using either? Both are legal, and in both cases the return value of the function is a Future that resolves to null . The following code prints null two times: import 'dart:async'; Future<void> someAsync() async {} Future<Null> otherAsync() async {} main() { someAsync().then((v) => print(v)); otherAsync().then((v) => print(v));

Dart 2: Difference between Future<void> and Future<Null>

匆匆过客 提交于 2020-05-25 06:15:21
问题 Having an asynchronous function that doesn't return a value, what's the ideal return type Future<Null> or Future<void> ?, or more specifically, what's the difference in using either? Both are legal, and in both cases the return value of the function is a Future that resolves to null . The following code prints null two times: import 'dart:async'; Future<void> someAsync() async {} Future<Null> otherAsync() async {} main() { someAsync().then((v) => print(v)); otherAsync().then((v) => print(v));

What is the true meaning of pass-by-reference in modern languages like Dart?

落爺英雄遲暮 提交于 2020-05-15 08:18:07
问题 Working with Futures in Dart, I've come across an interesting issue. import 'dart:async'; class Egg { String style; Egg(this.style); } Future cookEggs(List<Egg> list) => new Future(() => ['omelette','over easy'].forEach((_) => list.add(new Egg(_))) ); Future cookOne(Egg egg) => new Future(() => egg = new Egg('scrambled')); void main() { List<Egg> eggList = new List(); Egg single; cookEggs(eggList).whenComplete(() => eggList.forEach((_) => print(_.style)); cookOne(single).whenComplete(() =>

asio use_future instead of yield[ec]

随声附和 提交于 2020-05-13 14:35:19
问题 i want to make container of futures ,each future is void result of a task so that i could use wait_for_any on the container ,each task is coroutine which i currently implement using yield_context,and inside this coroutine there initiating function which returns ec and result where i use ec to analyze result.and then another coroutine is called passes same yield_context . i want to know how to make this design. and if i ll use use_future ,how can i pass error code to ec not throwing it unless

java future模式 所线程实现异步调用

戏子无情 提交于 2020-03-23 14:05:13
3 月,跳不动了?>>> 在多线程交互的中,经常有一个线程需要得到另个一线程的计算结果,我们常用的是Future异步模式来加以解决。 Future顾名思意,有点像期货市场的“期权”,是“对未来的一种凭证”,例如当我们买了某个房地产开发商的期房,交钱之后,开发商会给我们一个凭证(期权),这个凭证告诉我们等明年某个时候拿这个凭证就可以拿到我们所需要的房子,但是现在房子还没建好。市场上之所以有“期货”,也正由于有这种需求,才有这种供给。 这种应用在GUI上用的比较多,在设计模式中一般称为“虚拟代理模式”。 例如:现在有个这样的需求,Client向Server提交一个Request(int count,char c),希望获取一个由count个字符c构造出来的字符串。比如发送Request(10,'K'),那么反馈字符串“KKKKKKKKKK”,但是我们假设这个生成字符串的过程很费时间。 于是,为了获取比较好的交互性,我们的Server收到请求后,先构造一个FutureData,并把这个所谓的“期权(未来凭证)”反馈给Client;于此同时,通过另一个并发线程去构造一个真正的字符串RealData,并在构造完毕后,RealData给FutureData报告一个消息,说数据(期房)已经准备好了,此时Client可以通过期权拿到期房,但是假如我们的Client比较着急,还没等房子假好的时

并发编程之Callable和Future接口、FutureTask类

末鹿安然 提交于 2020-03-17 12:29:30
某厂面试归来,发现自己落伍了!>>> Callable接口代表一段可以调用并返回结果的代码;Future接口表示异步任务,是还没有完成的任务给出的未来结果。所以说Callable用于产生结果,Future用于获取结果。 Java 5在concurrency包中引入了java.util.concurrent.Callable 接口,它和Runnable接口很相似,但它可以返回一个对象或者抛出一个异常。 其中Runnable可以提交给Thread来包装下,直接启动一个线程来执行,而Callable则一般都是提交给ExecuteService来执行。Executor就是Runnable和Callable的调度容器,Future就是对于具体的调度任务的执行结果进行查看,最为关键的是Future可以检查对应的任务是否已经完成,也可以阻塞在get方法上一直等待任务返回结果。 Runnable和Callable的差别 就是Runnable是没有结果可以返回的,并且Runnable无法抛出返回结果的异常,就算是通过Future也看不到任务调度的结果的。 Callable接口使用泛型去定义它的返回类型。Executors类提供了一些有用的方法在线程池中执行Callable内的任务。由于 Callable任务是并行的(并行就是整体看上去是并行的,其实在某个时间点只有一个线程在执行)