diff --git a/lightproc/examples/proc_panic.rs b/lightproc/examples/proc_panic.rs index 4c395145..01a82e34 100644 --- a/lightproc/examples/proc_panic.rs +++ b/lightproc/examples/proc_panic.rs @@ -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(future: F) -> RecoverableHandle - where - F: Future + Send + 'static, - R: Send + 'static, +where + F: Future + Send + 'static, + R: Send + 'static, { lazy_static! { // A channel that holds scheduled tasks. @@ -47,7 +46,7 @@ fn spawn_on_thread(future: F) -> RecoverableHandle })), after_panic: Some(Arc::new(|| { println!("After panic"); - })) + })), }, ); diff --git a/lightproc/examples/proc_run.rs b/lightproc/examples/proc_run.rs index 2c539d20..670cce14 100644 --- a/lightproc/examples/proc_run.rs +++ b/lightproc/examples/proc_run.rs @@ -35,7 +35,7 @@ where before_start: Some(Arc::new(|| { println!("Before start"); })), - after_panic: None + after_panic: None, }, ); diff --git a/lightproc/src/catch_unwind.rs b/lightproc/src/catch_unwind.rs index 5857eb58..00e4de50 100644 --- a/lightproc/src/catch_unwind.rs +++ b/lightproc/src/catch_unwind.rs @@ -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 where Fut: Future { +pub struct CatchUnwind +where + Fut: Future, +{ future: Fut, } -impl CatchUnwind where Fut: Future + UnwindSafe { +impl CatchUnwind +where + Fut: Future + UnwindSafe, +{ unsafe_pinned!(future: Fut); pub(super) fn new(future: Fut) -> CatchUnwind { @@ -20,7 +26,8 @@ impl CatchUnwind where Fut: Future + UnwindSafe { } impl Future for CatchUnwind - where Fut: Future + UnwindSafe, +where + Fut: Future + UnwindSafe, { type Output = Result>; diff --git a/lightproc/src/lib.rs b/lightproc/src/lib.rs index b451c0da..a90e0ac4 100644 --- a/lightproc/src/lib.rs +++ b/lightproc/src/lib.rs @@ -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::*; diff --git a/lightproc/src/lightproc.rs b/lightproc/src/lightproc.rs index 1caacb3f..4efc472f 100644 --- a/lightproc/src/lightproc.rs +++ b/lightproc/src/lightproc.rs @@ -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. @@ -22,18 +22,21 @@ unsafe impl Send for LightProc {} unsafe impl Sync for LightProc {} impl LightProc { - pub fn recoverable(future: F, schedule: S, stack: ProcStack) -> (LightProc, RecoverableHandle) - where - F: Future + Send + 'static, - R: Send + 'static, - S: Fn(LightProc) + Send + Sync + 'static, + pub fn recoverable( + future: F, + schedule: S, + stack: ProcStack, + ) -> (LightProc, RecoverableHandle) + where + F: Future + 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(future: F, schedule: S, stack: ProcStack) -> (LightProc, ProcHandle) where F: Future + Send + 'static, diff --git a/lightproc/src/proc_ext.rs b/lightproc/src/proc_ext.rs index 187cbed1..4fb5c319 100644 --- a/lightproc/src/proc_ext.rs +++ b/lightproc/src/proc_ext.rs @@ -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 - where Self: Sized + UnwindSafe + where + Self: Sized + UnwindSafe, { CatchUnwind::new(self) } diff --git a/lightproc/src/proc_handle.rs b/lightproc/src/proc_handle.rs index 8cdfeee5..6124ae53 100644 --- a/lightproc/src/proc_handle.rs +++ b/lightproc/src/proc_handle.rs @@ -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::*; @@ -243,7 +243,6 @@ impl Future for ProcHandle { } } - impl fmt::Debug for ProcHandle { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let ptr = self.raw_proc.as_ptr(); diff --git a/lightproc/src/proc_stack.rs b/lightproc/src/proc_stack.rs index 68094e98..679c9d99 100644 --- a/lightproc/src/proc_stack.rs +++ b/lightproc/src/proc_stack.rs @@ -12,7 +12,7 @@ pub struct ProcStack { // After action callbacks pub after_complete: Option>, - + pub after_panic: Option>, } diff --git a/lightproc/src/raw_proc.rs b/lightproc/src/raw_proc.rs index a06ce190..96064a0f 100644 --- a/lightproc/src/raw_proc.rs +++ b/lightproc/src/raw_proc.rs @@ -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 = CatchUnwind>; @@ -31,8 +31,7 @@ pub(crate) struct RawProc { impl Copy for RawProc {} -impl Clone for RawProc -{ +impl Clone for RawProc { fn clone(&self) -> Self { Self { pdata: self.pdata, diff --git a/lightproc/src/recoverable_handle.rs b/lightproc/src/recoverable_handle.rs index 11cdc7f3..d6a9ca6f 100644 --- a/lightproc/src/recoverable_handle.rs +++ b/lightproc/src/recoverable_handle.rs @@ -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(pub ProcHandle>); @@ -21,7 +21,7 @@ impl Future for RecoverableHandle { } resume_unwind(err) - }, + } } } }