Skip to main content

SbiRet

Struct SbiRet 

Source
#[repr(C)]
pub struct SbiRet<T = usize> { pub error: T, pub value: T, }
Expand description

SBI functions return type.

SBI functions must return a pair of values in a0 and a1, with a0 returning an error code. This is analogous to returning the C structure SbiRet.

Note: if this structure is used in function return on conventional Rust code, it would not require pinning memory representation as extern C. The repr(C) is set in case that some users want to use this structure in FFI code.

Fields§

§error: T

Error number.

§value: T

Result value.

Implementations§

Source§

impl<T> SbiRet<T>
where T: SbiRegister,

Source

pub const fn success(value: T) -> SbiRet<T>

Returns success SBI state with given value.

Source

pub const fn failed() -> SbiRet<T>

The SBI call request failed for unknown reasons.

Source

pub const fn not_supported() -> SbiRet<T>

SBI call failed due to not supported by target ISA, operation type not supported, or target operation type not implemented on purpose.

Source

pub const fn invalid_param() -> SbiRet<T>

SBI call failed due to invalid hart mask parameter, invalid target hart id, invalid operation type, or invalid resource index.

Source

pub const fn denied() -> SbiRet<T>

SBI call denied for unsatisfied entry criteria, or insufficient access permission to debug console or CPPC register.

Source

pub const fn invalid_address() -> SbiRet<T>

SBI call failed for invalid mask start address, not a valid physical address parameter, or the target address is prohibited by PMP to run in supervisor mode.

Source

pub const fn already_available() -> SbiRet<T>

SBI call failed for the target resource is already available, e.g., the target hart is already started when caller still requests it to start.

Source

pub const fn already_started() -> SbiRet<T>

SBI call failed for the target resource is already started, e.g., target performance counter is started.

Source

pub const fn already_stopped() -> SbiRet<T>

SBI call failed for the target resource is already stopped, e.g., target performance counter is stopped.

Source

pub const fn no_shmem() -> SbiRet<T>

SBI call failed for shared memory is not available, e.g. nested acceleration shared memory is not available.

Source

pub const fn invalid_state() -> SbiRet<T>

SBI call failed for invalid state, e.g. register a software event but the event is not in unused state.

Source

pub const fn bad_range() -> SbiRet<T>

SBI call failed for bad or invalid range, e.g. the software event is not exist in the specified range.

Source

pub const fn timeout() -> SbiRet<T>

SBI call failed for timeout, e.g. message send timeout.

Source

pub const fn io() -> SbiRet<T>

SBI call failed for input or output error.

Source

pub const fn denied_locked() -> SbiRet<T>

SBI call failed for denied or not allowed due to lock status.

Source§

impl SbiRet

Source

pub const fn into_result(self) -> Result<usize, Error>

Converts to a Result of value and error.

Source

pub const fn is_ok(&self) -> bool

Returns true if current SBI return succeeded.

§Examples

Basic usage:

let x = SbiRet::success(0);
assert_eq!(x.is_ok(), true);

let x = SbiRet::failed();
assert_eq!(x.is_ok(), false);
Source

pub fn is_ok_and(self, f: impl FnOnce(usize) -> bool) -> bool

Returns true if the SBI call succeeded and the value inside of it matches a predicate.

§Examples

Basic usage:

let x = SbiRet::success(2);
assert_eq!(x.is_ok_and(|x| x > 1), true);

let x = SbiRet::success(0);
assert_eq!(x.is_ok_and(|x| x > 1), false);

let x = SbiRet::no_shmem();
assert_eq!(x.is_ok_and(|x| x > 1), false);
Source

pub const fn is_err(&self) -> bool

Returns true if current SBI return is an error.

§Examples

Basic usage:

let x = SbiRet::success(0);
assert_eq!(x.is_err(), false);

let x = SbiRet::not_supported();
assert_eq!(x.is_err(), true);
Source

pub fn is_err_and(self, f: impl FnOnce(Error) -> bool) -> bool

Returns true if the result is an error and the value inside of it matches a predicate.

§Examples
let x = SbiRet::denied();
assert_eq!(x.is_err_and(|x| x == Error::Denied), true);

let x = SbiRet::invalid_address();
assert_eq!(x.is_err_and(|x| x == Error::Denied), false);

let x = SbiRet::success(0);
assert_eq!(x.is_err_and(|x| x == Error::Denied), false);
Source

pub fn ok(self) -> Option<usize>

Converts from SbiRet to Option<usize>.

Converts self into an Option<usize>, consuming self, and discarding the error, if any.

§Examples

Basic usage:

let x = SbiRet::success(2);
assert_eq!(x.ok(), Some(2));

let x = SbiRet::invalid_param();
assert_eq!(x.ok(), None);
Source

pub fn err(self) -> Option<Error>

Converts from SbiRet to Option<Error>.

Converts self into an Option<Error>, consuming self, and discarding the success value, if any.

§Examples

Basic usage:

let x = SbiRet::success(2);
assert_eq!(x.err(), None);

let x = SbiRet::denied();
assert_eq!(x.err(), Some(Error::Denied));
Source

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

Maps a SbiRet to Result<U, Error> by applying a function to a contained success value, leaving an error value untouched.

This function can be used to compose the results of two functions.

§Examples

Gets detail of a PMU counter and judge if it is a firmware counter.

// We assume that counter index 42 is a firmware counter.
let counter_idx = 42;
// Masks PMU counter type by setting highest bit in `usize`.
const TYPE_MASK: usize = 1 << (size_of::<usize>() - 1);
// Highest bit of returned `counter_info` represents whether it's
// a firmware counter or a hardware counter.
let is_firmware_counter = sbi_rt::pmu_counter_get_info(counter_idx)
    .map(|counter_info| counter_info & TYPE_MASK != 0);
// If that bit is set, it is a firmware counter.
assert_eq!(is_firmware_counter, Ok(true));
Source

pub fn map_or<U, F>(self, default: U, f: F) -> U
where F: FnOnce(usize) -> U,

Returns the provided default (if error), or applies a function to the contained value (if success).

Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use map_or_else, which is lazily evaluated.

§Examples
let x = SbiRet::success(3);
assert_eq!(x.map_or(42, |v| v & 0b1), 1);

let x = SbiRet::invalid_address();
assert_eq!(x.map_or(42, |v| v & 0b1), 42);
Source

pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
where D: FnOnce(Error) -> U, F: FnOnce(usize) -> U,

Maps a SbiRet to usize value by applying fallback function default to a contained error, or function f to a contained success value.

This function can be used to unpack a successful result while handling an error.

§Examples

Basic usage:

let k = 21;

let x = SbiRet::success(3);
assert_eq!(x.map_or_else(|e| k * 2, |v| v & 0b1), 1);

let x = SbiRet::already_available();
assert_eq!(x.map_or_else(|e| k * 2, |v| v & 0b1), 42);
Source

pub fn map_err<F, O>(self, op: O) -> Result<usize, F>
where O: FnOnce(Error) -> F,

Maps a SbiRet to Result<T, F> by applying a function to a contained error as Error struct, leaving success value untouched.

This function can be used to pass through a successful result while handling an error.

§Examples

Basic usage:

fn stringify(x: Error) -> String {
    if x == Error::AlreadyStarted {
        "error: already started!".to_string()
    } else {
        "error: other error!".to_string()
    }
}

let x = SbiRet::success(2);
assert_eq!(x.map_err(stringify), Ok(2));

let x = SbiRet::already_started();
assert_eq!(x.map_err(stringify), Err("error: already started!".to_string()));
Source

pub fn inspect<F>(self, f: F) -> SbiRet
where F: FnOnce(&usize),

Calls a function with a reference to the contained value if current SBI call succeeded.

Returns the original result.

§Examples
// Assume that SBI debug console have read 512 bytes into a buffer.
let ret = SbiRet::success(512);
// Inspect the SBI DBCN call result.
let idx = ret
    .inspect(|x| println!("bytes written: {x}"))
    .map(|x| x - 1)
    .expect("SBI DBCN call failed");
assert_eq!(idx, 511);
Source

pub fn inspect_err<F>(self, f: F) -> SbiRet
where F: FnOnce(&Error),

Calls a function with a reference to the contained value if current SBI result is an error.

Returns the original result.

§Examples
// Assume that SBI debug console write operation failed for invalid parameter.
let ret = SbiRet::invalid_param();
// Print the error if SBI DBCN call failed.
let ret = ret.inspect_err(|e| eprintln!("failed to read from SBI console: {e:?}"));
Source

pub fn expect(self, msg: &str) -> usize

Returns the contained success value, consuming the self value.

§Panics

Panics if self is an SBI error with a panic message including the passed message, and the content of the SBI state.

§Examples

Basic usage:

let x = SbiRet::already_stopped();
x.expect("Testing expect"); // panics with `Testing expect`
Source

pub fn unwrap(self) -> usize

Returns the contained success value, consuming the self value.

§Panics

Panics if self is an SBI error, with a panic message provided by the SBI error converted into Error struct.

§Examples

Basic usage:

let x = SbiRet::success(2);
assert_eq!(x.unwrap(), 2);
let x = SbiRet::failed();
x.unwrap(); // panics
Source

pub fn expect_err(self, msg: &str) -> Error

Returns the contained error as Error struct, consuming the self value.

§Panics

Panics if the self is SBI success value, with a panic message including the passed message, and the content of the success value.

§Examples

Basic usage:

let x = SbiRet::success(10);
x.expect_err("Testing expect_err"); // panics with `Testing expect_err`
Source

pub fn unwrap_err(self) -> Error

Returns the contained error as Error struct, consuming the self value.

§Panics

Panics if the self is SBI success value, with a custom panic message provided by the success value.

§Examples
let x = SbiRet::success(2);
x.unwrap_err(); // panics with `2`
let x = SbiRet::not_supported();
assert_eq!(x.unwrap_err(), Error::NotSupported);
Source

pub fn and<U>(self, res: Result<U, Error>) -> Result<U, Error>

Returns res if self is success value, otherwise otherwise returns the contained error of self as Error struct.

Arguments passed to and are eagerly evaluated; if you are passing the result of a function call, it is recommended to use and_then, which is lazily evaluated.

§Examples

Basic usage:

let x = SbiRet::success(2);
let y = SbiRet::invalid_param().into_result();
assert_eq!(x.and(y), Err(Error::InvalidParam));

let x = SbiRet::denied();
let y = SbiRet::success(3).into_result();
assert_eq!(x.and(y), Err(Error::Denied));

let x = SbiRet::invalid_address();
let y = SbiRet::already_available().into_result();
assert_eq!(x.and(y), Err(Error::InvalidAddress));

let x = SbiRet::success(4);
let y = SbiRet::success(5).into_result();
assert_eq!(x.and(y), Ok(5));
Source

pub fn and_then<U, F>(self, op: F) -> Result<U, Error>
where F: FnOnce(usize) -> Result<U, Error>,

Calls op if self is success value, otherwise returns the contained error as Error struct.

This function can be used for control flow based on SbiRet values.

§Examples
fn sq_then_to_string(x: usize) -> Result<String, Error> {
    x.checked_mul(x).map(|sq| sq.to_string()).ok_or(Error::Failed)
}

assert_eq!(SbiRet::success(2).and_then(sq_then_to_string), Ok(4.to_string()));
assert_eq!(SbiRet::success(1_000_000_000_000).and_then(sq_then_to_string), Err(Error::Failed));
assert_eq!(SbiRet::invalid_param().and_then(sq_then_to_string), Err(Error::InvalidParam));
Source

pub fn or<F>(self, res: Result<usize, F>) -> Result<usize, F>

Returns res if self is SBI error, otherwise returns the success value of self.

Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use or_else, which is lazily evaluated.

§Examples

Basic usage:

let x = SbiRet::success(2);
let y = SbiRet::invalid_param().into_result();
assert_eq!(x.or(y), Ok(2));

let x = SbiRet::denied();
let y = SbiRet::success(3).into_result();
assert_eq!(x.or(y), Ok(3));

let x = SbiRet::invalid_address();
let y = SbiRet::already_available().into_result();
assert_eq!(x.or(y), Err(Error::AlreadyAvailable));

let x = SbiRet::success(4);
let y = SbiRet::success(100).into_result();
assert_eq!(x.or(y), Ok(4));
Source

pub fn or_else<F, O>(self, op: O) -> Result<usize, F>
where O: FnOnce(Error) -> Result<usize, F>,

Calls op if self is SBI error, otherwise returns the success value of self.

This function can be used for control flow based on result values.

§Examples

Basic usage:

fn is_failed(x: Error) -> Result<usize, bool> { Err(x == Error::Failed) }

assert_eq!(SbiRet::success(2).or_else(is_failed), Ok(2));
assert_eq!(SbiRet::failed().or_else(is_failed), Err(true));
Source

pub fn unwrap_or(self, default: usize) -> usize

Returns the contained success value or a provided default.

Arguments passed to unwrap_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrap_or_else, which is lazily evaluated.

§Examples

Basic usage:

let default = 2;
let x = SbiRet::success(9);
assert_eq!(x.unwrap_or(default), 9);

let x = SbiRet::invalid_param();
assert_eq!(x.unwrap_or(default), default);
Source

pub fn unwrap_or_else<F>(self, op: F) -> usize
where F: FnOnce(Error) -> usize,

Returns the contained success value or computes it from a closure.

§Examples

Basic usage:

fn invalid_use_zero(x: Error) -> usize { if x == Error::InvalidParam { 0 } else { 3 } }

assert_eq!(SbiRet::success(2).unwrap_or_else(invalid_use_zero), 2);
assert_eq!(SbiRet::invalid_param().unwrap_or_else(invalid_use_zero), 0);
Source

pub unsafe fn unwrap_unchecked(self) -> usize

Returns the contained success value, consuming the self value, without checking that the SbiRet contains an error value.

§Safety

Calling this method on an SbiRet containing an error value results in undefined behavior.

§Examples
let x = SbiRet::success(3);
assert_eq!(unsafe { x.unwrap_unchecked() }, 3);
let x = SbiRet::no_shmem();
unsafe { x.unwrap_unchecked(); } // Undefined behavior!
Source

pub unsafe fn unwrap_err_unchecked(self) -> Error

Returns the contained Error value, consuming the self value, without checking that the SbiRet does not contain a success value.

§Safety

Calling this method on an SbiRet containing a success value results in undefined behavior.

§Examples
let x = SbiRet::success(4);
unsafe { x.unwrap_unchecked(); } // Undefined behavior!
let x = SbiRet::failed();
assert_eq!(unsafe { x.unwrap_err_unchecked() }, Error::Failed);

Trait Implementations§

Source§

impl<T> Clone for SbiRet<T>
where T: Clone,

Source§

fn clone(&self) -> SbiRet<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for SbiRet<T>
where T: Copy,

Source§

impl<T> Debug for SbiRet<T>
where T: SbiRegister + LowerHex,

Source§

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

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

impl<T> Eq for SbiRet<T>
where T: Eq,

Source§

impl<T> From<Error<T>> for SbiRet<T>
where T: SbiRegister,

Source§

fn from(value: Error<T>) -> SbiRet<T>

Converts to this type from the input type.
Source§

impl IntoIterator for SbiRet

Source§

fn into_iter(self) -> <SbiRet as IntoIterator>::IntoIter

Returns a consuming iterator over the possibly contained value.

The iterator yields one value if the result contains a success value, otherwise none.

§Examples
let x = SbiRet::success(5);
let v: Vec<usize> = x.into_iter().collect();
assert_eq!(v, [5]);

let x = SbiRet::not_supported();
let v: Vec<usize> = x.into_iter().collect();
assert_eq!(v, []);
Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<usize>

Which kind of iterator are we turning this into?
Source§

impl<T> PartialEq for SbiRet<T>
where T: PartialEq,

Source§

fn eq(&self, other: &SbiRet<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T> StructuralPartialEq for SbiRet<T>
where T: PartialEq,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnsafeUnpin for SbiRet<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for SbiRet<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.