-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add rustdocs to structs, enums and methods
- Loading branch information
Showing
4 changed files
with
771 additions
and
439 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::{any::type_name, fmt, ops::Deref, sync::Arc}; | ||
|
||
use crate::Process; | ||
|
||
/// A thin wrapper around `Arc<E>` with custom `fmt::Debug` and `fmt::Display` | ||
/// implementations for better error logging. | ||
pub struct SharedErr<E> { | ||
err: Arc<E>, | ||
} | ||
|
||
impl<E> SharedErr<E> { | ||
pub fn new(e: E) -> Self { | ||
Self { err: Arc::new(e) } | ||
} | ||
} | ||
|
||
impl<E> Clone for SharedErr<E> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
err: self.err.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<E> Deref for SharedErr<E> { | ||
type Target = E; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
let name = type_name::<E>(); | ||
println!("deref called on {name}!"); | ||
self.err.deref() | ||
} | ||
} | ||
|
||
impl<E> AsRef<E> for SharedErr<E> { | ||
fn as_ref(&self) -> &E { | ||
let name = type_name::<E>(); | ||
println!("asref called on {name}!"); | ||
self.err.as_ref() | ||
} | ||
} | ||
|
||
impl<E> fmt::Debug for SharedErr<E> | ||
where | ||
E: fmt::Debug, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{:?}", self.err.as_ref()) | ||
} | ||
} | ||
|
||
impl<E> fmt::Display for SharedErr<E> | ||
where | ||
E: fmt::Display, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.err.as_ref()) | ||
} | ||
} | ||
|
||
pub enum ExitReason<P> | ||
where | ||
P: Process, | ||
{ | ||
/// Process exited due to manual request through a `Handle<P>` | ||
Handle, | ||
/// Process exited due to a request from its Parent process as a part of its supervision strategy. | ||
Parent, | ||
/// Procss exited due to error. | ||
Err(SharedErr<P::Err>), | ||
} | ||
|
||
impl<P> fmt::Debug for ExitReason<P> | ||
where | ||
P: Process, | ||
P::Err: fmt::Debug, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::Handle => write!(f, "ExitReason::Handle"), | ||
Self::Parent => write!(f, "ExitReason::Parent"), | ||
Self::Err(arg0) => write!(f, "ExitReason::Err({:?})", arg0), | ||
} | ||
} | ||
} | ||
|
||
impl<P> fmt::Display for ExitReason<P> | ||
where | ||
P: Process, | ||
P::Err: fmt::Display, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::Handle => write!(f, "manual exit through Handle::stop"), | ||
Self::Parent => write!(f, "exit request from parent supervision strategy"), | ||
Self::Err(e) => write!(f, "{e}"), | ||
} | ||
} | ||
} |
Oops, something went wrong.