-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a future which can avoid creating an event loop for sync calls
- Loading branch information
Showing
5 changed files
with
78 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use futures::{Async, Future, Poll}; | ||
|
||
use Error; | ||
|
||
pub enum FutureValue<F> | ||
where F: Future, | ||
{ | ||
Value(F::Item), | ||
Future(F), | ||
Polled, | ||
} | ||
|
||
impl<F> FutureValue<F> | ||
where F: Future<Error = Error>, | ||
{ | ||
pub fn sync_or_error(self) -> Result<F::Item, Error> { | ||
match self { | ||
FutureValue::Value(v) => Ok(v), | ||
FutureValue::Future(_) => { | ||
Err(Error::Message("Future needs to be resolved asynchronously".into())) | ||
} | ||
FutureValue::Polled => { | ||
panic!("`FutureValue` may not be polled again if it contained a value") | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<F> Future for FutureValue<F> | ||
where F: Future, | ||
{ | ||
type Item = F::Item; | ||
type Error = F::Error; | ||
|
||
fn poll(&mut self) -> Poll<F::Item, F::Error> { | ||
match *self { | ||
FutureValue::Value(_) => { | ||
match ::std::mem::replace(self, FutureValue::Polled) { | ||
FutureValue::Value(v) => return Ok(Async::Ready(v)), | ||
_ => unreachable!(), | ||
} | ||
} | ||
FutureValue::Future(ref mut f) => f.poll(), | ||
FutureValue::Polled => { | ||
panic!("`FutureValue` may not be polled again if it contained a value") | ||
} | ||
} | ||
} | ||
|
||
fn wait(self) -> Result<F::Item, F::Error> { | ||
match self { | ||
FutureValue::Value(v) => Ok(v), | ||
FutureValue::Future(f) => f.wait(), | ||
FutureValue::Polled => { | ||
panic!("`FutureValue` may not be polled again if it contained a value") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters