Skip to content

Commit

Permalink
Merge pull request #11 from aspcartman/main
Browse files Browse the repository at this point in the history
Make BoxSendError and it's content pub
  • Loading branch information
ChrisRega authored Apr 18, 2023
2 parents ac0ff40 + 38f592a commit 50df977
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
30 changes: 7 additions & 23 deletions src/immediatevalue.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
use crate::DirectCacheAccess;
use std::error::Error;
use std::future::Future;
use std::mem;
use std::ops::Deref;
use std::sync::Arc;
use tokio::sync::Mutex;

/// Strong type to keep the boxed error. You can just deref it to get the inside box.
pub struct BoxedSendError(Box<dyn Error + Send>);
type FutureResult<T> = Result<T, BoxedSendError>;

impl<E: Error + Send + 'static> From<E> for BoxedSendError {
fn from(e: E) -> Self {
BoxedSendError(Box::new(e))
}
}

impl Deref for BoxedSendError {
type Target = Box<dyn Error + Send>;
use tokio::sync::Mutex;

fn deref(&self) -> &Self::Target {
&self.0
}
}
use crate::{BoxedSendError, DirectCacheAccess, FutureResult};

/// # A promise which can be easily created and stored.
/// ## Introduction
Expand Down Expand Up @@ -180,7 +162,7 @@ impl<T: Send + 'static> DirectCacheAccess<T> for ImmediateValuePromise<T> {

impl<T: Send> ImmediateValuePromise<T> {
/// Creator, supply a future which returns `Result<T, Box<dyn Error + Send>`. Will be immediately spawned via tokio.
pub fn new<U: Future<Output = Result<T, BoxedSendError>> + Send + 'static>(updater: U) -> Self {
pub fn new<U: Future<Output=Result<T, BoxedSendError>> + Send + 'static>(updater: U) -> Self {
let arc = Arc::new(Mutex::new(None));
let arc_clone = arc.clone();
tokio::spawn(async move {
Expand Down Expand Up @@ -223,12 +205,14 @@ impl<T: Send> ImmediateValuePromise<T> {

#[cfg(test)]
mod test {
use crate::immediatevalue::{ImmediateValuePromise, ImmediateValueState};
use crate::DirectCacheAccess;
use std::fs::File;
use std::time::Duration;

use tokio::runtime::Runtime;

use crate::DirectCacheAccess;
use crate::immediatevalue::{ImmediateValuePromise, ImmediateValueState};

#[test]
fn default() {
Runtime::new().unwrap().block_on(async {
Expand Down
49 changes: 36 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,47 @@

extern crate core;

mod immediatevalue;
mod lazyvalue;
mod lazyvec;
use std::error::Error;
use std::fmt::Debug;
use std::future::Future;
use std::ops::Deref;
use std::pin::Pin;

#[doc(inline)]
pub use lazyvec::LazyVecPromise;
use tokio::sync::mpsc::Sender;

#[doc(inline)]
pub use immediatevalue::ImmediateValuePromise;
pub use immediatevalue::ImmediateValueState;

#[doc(inline)]
pub use lazyvalue::LazyValuePromise;
#[doc(inline)]
pub use lazyvec::LazyVecPromise;

mod immediatevalue;
mod lazyvalue;
mod lazyvec;

/// Strong type to keep the boxed error. You can just deref it to get the inside box.
pub struct BoxedSendError(pub Box<dyn Error + Send>);


/// Type alias for futures with BoxedSendError
pub type FutureResult<T> = Result<T, BoxedSendError>;

impl<E: Error + Send + 'static> From<E> for BoxedSendError {
fn from(e: E) -> Self {
BoxedSendError(Box::new(e))
}
}

impl Deref for BoxedSendError {
type Target = Box<dyn Error + Send>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use tokio::sync::mpsc::Sender;

/// Trait for directly accessing the cache underneath any promise
pub trait DirectCacheAccess<T> {
Expand Down Expand Up @@ -71,12 +94,12 @@ impl<T: Into<f64>> From<T> for Progress {

/// Use this to get all macros
pub mod api_macros {
pub use crate::Progress;
pub use crate::send_data;
pub use crate::set_error;
pub use crate::set_finished;
pub use crate::set_progress;
pub use crate::unpack_result;
pub use crate::Progress;
}

impl Progress {
Expand Down Expand Up @@ -220,12 +243,12 @@ macro_rules! set_finished {
}

type BoxedFutureFactory<T> =
Box<dyn Fn(Sender<Message<T>>) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>>;
Box<dyn Fn(Sender<Message<T>>) -> Pin<Box<dyn Future<Output=()> + Send + 'static>>>;

fn box_future_factory<
T: Debug,
U: Fn(Sender<Message<T>>) -> Fut + 'static,
Fut: Future<Output = ()> + Send + 'static,
Fut: Future<Output=()> + Send + 'static,
>(
future_factory: U,
) -> BoxedFutureFactory<T> {
Expand Down

0 comments on commit 50df977

Please sign in to comment.