Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Context impl #224

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions crates/musli/src/context/capture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
use core::cell::UnsafeCell;
use core::error::Error;
use core::fmt;
use core::marker::PhantomData;

use super::{ContextError, ErrorMarker};

mod sealed {
pub trait Sealed {}
impl Sealed for super::NoCapture {}
impl<E, A> Sealed for super::CaptureError<E, A> {}
impl<E, A> Sealed for super::SameError<E, A> {}
}

/// The trait governing how error capture is implemented.
///
/// See [`DefaultContext::with_capture`] or [`DefaultContext::with_same`] for
/// more information.
///
/// [`DefaultContext::with_capture`]: super::DefaultContext::with_capture
/// [`DefaultContext::with_same`]: super::DefaultContext::with_same
pub trait Capture<A>
where
Self: self::sealed::Sealed,
{
#[doc(hidden)]
type Error;

#[doc(hidden)]
fn clear(&self);

#[doc(hidden)]
fn message<T>(&self, alloc: A, message: T) -> Self::Error
where
T: fmt::Display;

#[doc(hidden)]
fn custom<T>(&self, alloc: A, error: T) -> Self::Error
where
T: 'static + Send + Sync + Error;
}

/// Disable error capture.
#[non_exhaustive]
pub struct NoCapture;

impl<A> Capture<A> for NoCapture {
type Error = ErrorMarker;

#[inline]
fn clear(&self) {}

#[inline]
fn message<T>(&self, alloc: A, message: T) -> Self::Error
where
T: fmt::Display,
{
_ = alloc;
_ = message;
ErrorMarker
}

#[inline]
fn custom<T>(&self, alloc: A, error: T) -> Self::Error
where
T: 'static + Send + Sync + Error,
{
_ = alloc;
_ = error;
ErrorMarker
}
}

/// Capture an error of the specified type.
///
/// See [`DefaultContext::with_same`] for more information.
///
/// [`DefaultContext::with_same`]: super::DefaultContext::with_same
pub struct SameError<E, A> {
_marker: PhantomData<(E, A)>,
}

impl<E, A> SameError<E, A> {
#[inline]
pub(super) fn new() -> Self {
Self {
_marker: PhantomData,
}
}
}

impl<E, A> Capture<A> for SameError<E, A>
where
E: ContextError<A>,
{
type Error = E;

#[inline]
fn clear(&self) {}

#[inline]
fn message<T>(&self, alloc: A, message: T) -> Self::Error
where
T: fmt::Display,
{
E::message(alloc, message)
}

#[inline]
fn custom<T>(&self, alloc: A, error: T) -> Self::Error
where
T: 'static + Send + Sync + Error,
{
E::custom(alloc, error)
}
}

/// Capture an error of the specified type.
///
/// See [`DefaultContext::with_capture`] for more information.
///
/// [`DefaultContext::with_capture`]: super::DefaultContext::with_capture
pub struct CaptureError<E, A> {
error: UnsafeCell<Option<E>>,
_marker: PhantomData<A>,
}

impl<E, A> CaptureError<E, A> {
#[inline]
pub(super) fn new() -> Self {
Self {
error: UnsafeCell::new(None),
_marker: PhantomData,
}
}
}

impl<E, A> CaptureError<E, A> {
#[inline]
pub(super) fn unwrap(&self) -> E {
// SAFETY: We're restricting access to the context, so that this is
// safe.
unsafe {
match (*self.error.get()).take() {
Some(error) => error,
None => panic!("no error captured"),
}
}
}

#[inline]
pub(super) fn result(&self) -> Result<(), E> {
// SAFETY: We're restricting access to the context, so that this is
// safe.
unsafe {
match (*self.error.get()).take() {
Some(error) => Err(error),
None => Ok(()),
}
}
}
}

impl<E, A> Capture<A> for CaptureError<E, A>
where
E: ContextError<A>,
{
type Error = ErrorMarker;

#[inline]
fn clear(&self) {
// SAFETY: We're restricting access to the context, so that this is
// safe.
unsafe {
(*self.error.get()) = None;
}
}

#[inline]
fn message<T>(&self, alloc: A, message: T) -> Self::Error
where
T: fmt::Display,
{
// SAFETY: We're restricting access to the context, so that this is
// safe.
unsafe {
(*self.error.get()) = Some(E::message(alloc, message));
}

ErrorMarker
}

#[inline]
fn custom<T>(&self, alloc: A, error: T) -> Self::Error
where
T: 'static + Send + Sync + Error,
{
// SAFETY: We're restricting access to the context, so that this is
// safe.
unsafe {
(*self.error.get()) = Some(E::custom(alloc, error));
}

ErrorMarker
}
}
2 changes: 2 additions & 0 deletions crates/musli/src/context/context_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ pub trait ContextError<A> {

#[cfg(feature = "std")]
impl<A> ContextError<A> for std::io::Error {
#[inline]
fn custom<T>(_: A, message: T) -> Self
where
T: 'static + Send + Sync + Error,
{
std::io::Error::new(std::io::ErrorKind::Other, message)
}

#[inline]
fn message<T>(_: A, message: T) -> Self
where
T: fmt::Display,
Expand Down
Loading
Loading