Struct Error

Source
pub struct Error<T> { /* private fields */ }
Expand description

The error type for scratchpad operations.

Various scratchpad operations require ownership of some or all of their parameters to be transferred to the callee, such as for storage in a new allocation. If such operations fail, the caller may still want to do something with the data originally provided.

This type encapsulates both the kind of error that occurred and any recoverable “owned” parameters that were passed so that the caller can have them back.

Implementations§

Source§

impl<T> Error<T>

Source

pub fn new(kind: ErrorKind, args: T) -> Self

Creates a new error with the specified category and argument values.

§Examples
use scratchpad::{Error, ErrorKind};

// Error containing multiple recycled arguments.
let multi_error = Error::new(
    ErrorKind::MarkerLocked,
    (3.14159f32, 2.71828f32),
);

// Error containing a single recycled argument. A tuple is still used
// to remain consistent with errors that recycle multiple argument
// values.
let single_error = Error::new(ErrorKind::MarkerLocked, (3.14159f32,));

// Error containing no recycled argument values. A unit is used by the
// crate to signify an empty set of arguments.
let simple_error = Error::new(ErrorKind::MarkerLocked, ());
Source

pub fn kind(&self) -> ErrorKind

Returns the category of error that occurred.

§Examples
use scratchpad::{Error, ErrorKind};

let error = Error::new(
    ErrorKind::MarkerLocked,
    (3.14159f32, 2.71828f32),
);

let kind = error.kind();
assert_eq!(kind, ErrorKind::MarkerLocked);
Source

pub fn args(&self) -> &T

Returns a tuple containing values that would have passed ownership to the callee if the operation was successful.

While there’s no constraint on the arguments type, this crate will only ever produce errors that have either a unit or tuple for its arguments, even if only a single value is returned to the caller.

§Examples
use scratchpad::{Error, ErrorKind};

let error = Error::new(
    ErrorKind::MarkerLocked,
    (3.14159f32, 2.71828f32),
);

let args = error.args();
assert_eq!(args, &(3.14159f32, 2.71828f32));
Source

pub fn unwrap_args(self) -> T

Unwraps the error, yielding a tuple containing values that would have passed ownership to the callee if the operation was successful.

§Examples
use scratchpad::{Error, ErrorKind};

let error = Error::new(
    ErrorKind::MarkerLocked,
    (3.14159f32, 2.71828f32),
);

let (x, y) = error.unwrap_args();
assert_eq!(x, 3.14159f32);
assert_eq!(y, 2.71828f32);
Source

pub fn map<U, F>(self, op: F) -> Error<U>
where F: FnOnce(T) -> U,

Maps an Error<T> to an Error<U> by applying a function to the contained arguments value, leaving the ErrorKind value untouched.

§Examples
use scratchpad::{Error, ErrorKind};

let error = Error::new(ErrorKind::MarkerLocked, (-12, 23));

let new_error = error.map(|args| (args.1 as i64 * -2,));
assert_eq!(new_error.args().0, -46i64);

Trait Implementations§

Source§

impl<T: Debug> Debug for Error<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for Error<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Error for Error<T>
where T: Debug,

Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Error<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Error<T>
where T: RefUnwindSafe,

§

impl<T> Send for Error<T>
where T: Send,

§

impl<T> Sync for Error<T>
where T: Sync,

§

impl<T> Unpin for Error<T>
where T: Unpin,

§

impl<T> UnwindSafe for Error<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoMutSliceLikePtr<[T]> for T

Source§

fn into_mut_slice_like_ptr(ptr: *mut T) -> *mut [T]

Reinterprets a mutable pointer of this type as a SliceLike pointer. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.