Res

Struct Res 

Source
pub struct Res(/* private fields */);
Expand description

An opaque result type for error handling without exposing error details.

This type is designed to prevent side-channel attacks by not revealing specific error information. It only indicates success or failure.

Implementations§

Source§

impl Res

Source

pub const OK: Self

Represents a successful result.

Source

pub const ERR: Self

Represents an error result.

Source

pub const fn new() -> Self

Creates a new Res instance initialized to OK.

§Returns

A new Res instance representing success.

Source

pub const fn is_ok(&self) -> bool

Checks if the result is OK (successful).

§Returns

true if the result is OK, false otherwise.

Source

pub const fn is_err(&self) -> bool

Checks if the result is an error.

§Returns

true if the result is an error, false otherwise.

Source

pub fn check(&mut self, res: bool)

Updates the result based on a boolean condition.

If res is false, this method will set the Res to an error state.

§Arguments
  • res - A boolean representing a condition to check.
Source

pub fn ensure_1(&mut self, res: c_int)

Ensures that a C integer result is equal to 1.

Sets the Res to an error state if the input is not 1.

§Arguments
  • res - A C integer to check.
Source

pub fn ensure_0(&mut self, res: c_int)

Ensures that a C integer result is equal to 0.

Sets the Res to an error state if the input is not 0.

§Arguments
  • res - A C integer to check.
Source

pub fn ensure_pos(&mut self, res: c_int)

Ensures that a C integer result is positive.

Sets the Res to an error state if the input is not positive.

§Arguments
  • res - A C integer to check.
Source

pub fn ensure(&mut self, res: Self)

Combines this Res with another Res.

The result will be OK only if both Res instances are OK.

§Arguments
  • res - Another Res instance to combine with this one.
Source

pub fn unit_err<OK>(self, ok: OK) -> Result<OK, Unspecified>

Converts the Res into a Result<OK, Unspecified>.

§Warning

This method is not constant time and should be used carefully in security-sensitive contexts.

§Arguments
  • ok - The value to return in the Ok variant if the Res is OK.
§Returns

Ok(ok) if the Res is OK, Err(Unspecified) otherwise.

Source

pub fn unit_err_with<F, OK>(self, ok: F) -> Result<OK, Unspecified>
where F: FnOnce() -> OK,

Converts the Res into a Result<OK, Unspecified>, with a closure for the OK case.

§Warning

This method is not constant time and should be used carefully in security-sensitive contexts.

§When to Use

Use this method when the creation of the OK value depends on the result being OK for safety reasons. The closure is only called if the Res is OK, ensuring that any preconditions for the OK value’s creation are met.

§Arguments
  • ok - A closure that returns the value for the Ok variant if the Res is OK.
§Returns

Ok(ok()) if the Res is OK, Err(Unspecified) otherwise.

Source

pub fn unwrap(self)

Unwraps the Res, panicking if it’s an error.

§Panics

Panics if the Res is an error.

§Warning

This method should generally be avoided in production code, as it can lead to program termination. It’s primarily useful for testing or in situations where an error truly represents an unrecoverable state.

Trait Implementations§

Source§

impl Debug for Res

Source§

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

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

impl Default for Res

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Res

§

impl RefUnwindSafe for Res

§

impl Send for Res

§

impl Sync for Res

§

impl Unpin for Res

§

impl UnwindSafe for Res

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, 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.