Skip to content
Snippets Groups Projects
Commit 8424caaf authored by Georgy Moshkin's avatar Georgy Moshkin :speech_balloon:
Browse files

feat(net_box): promise::TryGet::into_res

parent 2e6a6921
No related branches found
No related tags found
1 merge request!205Fix/tuple/push into lua
......@@ -25,6 +25,7 @@
- `tlua::LuaRead`, `tlua::Push` & `tlua::PushInto` derive macros now support
new-type style tuple structs: they are treated as the inner type.
- `impl tlua::PushInto for Tuple`.
- `net_box::promise::TryGet::into_res` and `From<TryGet<_, _>> for Result<_, _>`.
- Doc-comments here and there.
### Fixed
......
......@@ -194,8 +194,11 @@ pub enum State {
/// or [`Promise::wait_timeout`] methods.
#[derive(Debug)]
pub enum TryGet<T, E> {
/// Promise was kept successfully.
Ok(T),
/// Promise will never be kept due to an error.
Err(E),
/// Promise yet is unresolved.
Pending(Promise<T>),
}
......@@ -220,6 +223,26 @@ impl<T, E> TryGet<T, E> {
_ => None,
}
}
/// Converts `self` into a nested [`Result`].
///
/// Returns
/// - `Ok(Ok(value))` in case of [`TryGet::Ok`]`(value)`.
/// - `Ok(Err(error))` in case of [`TryGet::Err`]`(error)`.
/// - `Err(promise)` in case of [`TryGet::Pending`]`(promise)`.
///
/// This function basically checks if the promise is resolved (`Ok`) or not
/// yet (`Err`).
///
/// [`Result`]: std::result::Result
#[inline(always)]
pub fn into_res(self) -> StdResult<StdResult<T, E>, Promise<T>> {
match self {
Self::Ok(v) => Ok(Ok(v)),
Self::Err(e) => Ok(Err(e)),
Self::Pending(p) => Err(p),
}
}
}
impl<T, E> From<StdResult<T, E>> for TryGet<T, E> {
......@@ -231,6 +254,13 @@ impl<T, E> From<StdResult<T, E>> for TryGet<T, E> {
}
}
impl<T, E> From<TryGet<T, E>> for StdResult<StdResult<T, E>, Promise<T>> {
#[inline(always)]
fn from(r: TryGet<T, E>) -> Self {
r.into_res()
}
}
use std::fmt;
impl<T> fmt::Debug for Promise<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment