Skip to content

Commit

Permalink
Format all the code
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexclique committed Oct 24, 2019
1 parent a3aba3b commit 000d42e
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 44 deletions.
15 changes: 7 additions & 8 deletions lightproc/examples/proc_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ use std::thread;

use crossbeam::channel::{unbounded, Sender};
use futures::{executor, FutureExt};
use lightproc::prelude::*;
use std::sync::atomic::AtomicUsize;
use lazy_static::lazy_static;
use std::panic::AssertUnwindSafe;
use lightproc::prelude::*;
use lightproc::proc_ext::ProcFutureExt;
use lightproc::recoverable_handle::RecoverableHandle;

use std::panic::AssertUnwindSafe;
use std::sync::atomic::AtomicUsize;

fn spawn_on_thread<F, R>(future: F) -> RecoverableHandle<R>
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
lazy_static! {
// A channel that holds scheduled tasks.
Expand Down Expand Up @@ -47,7 +46,7 @@ fn spawn_on_thread<F, R>(future: F) -> RecoverableHandle<R>
})),
after_panic: Some(Arc::new(|| {
println!("After panic");
}))
})),
},
);

Expand Down
2 changes: 1 addition & 1 deletion lightproc/examples/proc_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
before_start: Some(Arc::new(|| {
println!("Before start");
})),
after_panic: None
after_panic: None,
},
);

Expand Down
19 changes: 13 additions & 6 deletions lightproc/src/catch_unwind.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use std::future::Future;
use std::panic::{UnwindSafe, catch_unwind, AssertUnwindSafe};
use pin_utils::unsafe_pinned;
use std::any::Any;
use std::task::{Context, Poll};
use std::future::Future;
use std::panic::{catch_unwind, AssertUnwindSafe, UnwindSafe};
use std::pin::Pin;
use std::task::{Context, Poll};

#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct CatchUnwind<Fut> where Fut: Future {
pub struct CatchUnwind<Fut>
where
Fut: Future,
{
future: Fut,
}

impl<Fut> CatchUnwind<Fut> where Fut: Future + UnwindSafe {
impl<Fut> CatchUnwind<Fut>
where
Fut: Future + UnwindSafe,
{
unsafe_pinned!(future: Fut);

pub(super) fn new(future: Fut) -> CatchUnwind<Fut> {
Expand All @@ -20,7 +26,8 @@ impl<Fut> CatchUnwind<Fut> where Fut: Future + UnwindSafe {
}

impl<Fut> Future for CatchUnwind<Fut>
where Fut: Future + UnwindSafe,
where
Fut: Future + UnwindSafe,
{
type Output = Result<Fut::Output, Box<dyn Any + Send>>;

Expand Down
8 changes: 4 additions & 4 deletions lightproc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
pub mod catch_unwind;
pub mod layout_helpers;
pub mod lightproc;
pub mod panic_helpers;
pub mod proc_data;
pub mod proc_ext;
pub mod proc_handle;
pub mod proc_layout;
pub mod proc_stack;
pub mod proc_vtable;
pub mod raw_proc;
pub mod state;
pub mod panic_helpers;
pub mod catch_unwind;
pub mod proc_ext;
pub mod recoverable_handle;
pub mod state;

pub mod prelude {
pub use crate::lightproc::*;
Expand Down
25 changes: 14 additions & 11 deletions lightproc/src/lightproc.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::{fmt, thread};
use std::future::Future;
use std::marker::PhantomData;
use std::mem;
use std::ptr::NonNull;
use std::{fmt, thread};

use crate::catch_unwind::CatchUnwind;
use crate::proc_data::ProcData;
use crate::proc_ext::ProcFutureExt;
use crate::proc_handle::ProcHandle;
use crate::proc_stack::*;
use crate::raw_proc::{RawProc, ProcFuture};
use std::panic::AssertUnwindSafe;
use crate::proc_ext::ProcFutureExt;
use crate::catch_unwind::CatchUnwind;
use crate::raw_proc::{ProcFuture, RawProc};
use crate::recoverable_handle::RecoverableHandle;
use std::panic::AssertUnwindSafe;

pub struct LightProc {
/// A pointer to the heap-allocated task.
Expand All @@ -22,18 +22,21 @@ unsafe impl Send for LightProc {}
unsafe impl Sync for LightProc {}

impl LightProc {
pub fn recoverable<F, R, S>(future: F, schedule: S, stack: ProcStack) -> (LightProc, RecoverableHandle<R>)
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
S: Fn(LightProc) + Send + Sync + 'static,
pub fn recoverable<F, R, S>(
future: F,
schedule: S,
stack: ProcStack,
) -> (LightProc, RecoverableHandle<R>)
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
S: Fn(LightProc) + Send + Sync + 'static,
{
let recovery_future = AssertUnwindSafe(future).catch_unwind();
let (proc, handle) = Self::build(recovery_future, schedule, stack);
(proc, RecoverableHandle(handle))
}


pub fn build<F, R, S>(future: F, schedule: S, stack: ProcStack) -> (LightProc, ProcHandle<R>)
where
F: Future<Output = R> + Send + 'static,
Expand Down
5 changes: 3 additions & 2 deletions lightproc/src/proc_ext.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::catch_unwind::CatchUnwind;
use std::future::Future;
use std::panic::UnwindSafe;
use crate::catch_unwind::CatchUnwind;

pub trait ProcFutureExt: Future {
fn catch_unwind(self) -> CatchUnwind<Self>
where Self: Sized + UnwindSafe
where
Self: Sized + UnwindSafe,
{
CatchUnwind::new(self)
}
Expand Down
3 changes: 1 addition & 2 deletions lightproc/src/proc_handle.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{fmt, mem};
use std::future::Future;
use std::marker::{PhantomData, Unpin};
use std::pin::Pin;
use std::ptr::NonNull;
use std::sync::atomic::Ordering;
use std::task::{Context, Poll};
use std::{fmt, mem};

use crate::proc_data::ProcData;
use crate::proc_stack::*;
Expand Down Expand Up @@ -243,7 +243,6 @@ impl<R> Future for ProcHandle<R> {
}
}


impl<R> fmt::Debug for ProcHandle<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ptr = self.raw_proc.as_ptr();
Expand Down
2 changes: 1 addition & 1 deletion lightproc/src/proc_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct ProcStack {

// After action callbacks
pub after_complete: Option<Arc<dyn Fn() + Send + Sync>>,

pub after_panic: Option<Arc<dyn Fn() + Send + Sync>>,
}

Expand Down
9 changes: 4 additions & 5 deletions lightproc/src/raw_proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use std::ptr::NonNull;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};

use crate::catch_unwind::CatchUnwind;
use crate::layout_helpers::extend;
use crate::lightproc::{LightProc};
use crate::lightproc::LightProc;
use crate::proc_data::ProcData;
use crate::proc_layout::TaskLayout;
use crate::proc_stack::*;
use crate::proc_vtable::ProcVTable;
use crate::state::*;
use std::panic::AssertUnwindSafe;
use crate::catch_unwind::CatchUnwind;
use std::any::Any;
use std::panic::AssertUnwindSafe;

pub type ProcFuture<F> = CatchUnwind<AssertUnwindSafe<F>>;

Expand All @@ -31,8 +31,7 @@ pub(crate) struct RawProc<F, R, S> {

impl<F, R, S> Copy for RawProc<F, R, S> {}

impl<F, R, S> Clone for RawProc<F, R, S>
{
impl<F, R, S> Clone for RawProc<F, R, S> {
fn clone(&self) -> Self {
Self {
pdata: self.pdata,
Expand Down
8 changes: 4 additions & 4 deletions lightproc/src/recoverable_handle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::proc_handle::ProcHandle;
use std::thread;
use std::future::Future;
use std::task::{Context, Poll};
use std::pin::Pin;
use std::panic::resume_unwind;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::thread;

pub struct RecoverableHandle<R>(pub ProcHandle<thread::Result<R>>);

Expand All @@ -21,7 +21,7 @@ impl<R> Future for RecoverableHandle<R> {
}

resume_unwind(err)
},
}
}
}
}

0 comments on commit 000d42e

Please sign in to comment.