pub trait IntoBinderResult<T, E> {
// Required methods
fn or_binder_exception(self, exception: ExceptionCode) -> Result<T, Status>;
fn or_binder_exception_with<M: AsRef<str>, O: FnOnce(E) -> M>(
self,
exception: ExceptionCode,
op: O,
) -> Result<T, Status>;
fn or_service_specific_exception(self, error_code: i32) -> Result<T, Status>;
fn or_service_specific_exception_with<M: AsRef<str>, O: FnOnce(E) -> M>(
self,
error_code: i32,
op: O,
) -> Result<T, Status>;
}
Expand description
A conversion from std::result::Result<T, E>
to binder::Result<T>
. If this type is Ok(T)
,
it’s returned as is. If this type is Err(E)
, E
is converted into Status
which can be
either a general binder exception, or a service-specific exception.
§Examples
// std::io::Error is formatted as the exception's message
fn file_exists(name: &str) -> binder::Result<bool> {
std::fs::metadata(name)
.or_service_specific_exception(NOT_FOUND)?
}
// A custom function is used to create the exception's message
fn file_exists(name: &str) -> binder::Result<bool> {
std::fs::metadata(name)
.or_service_specific_exception_with(NOT_FOUND,
|e| format!("file {} not found: {:?}", name, e))?
}
// anyhow::Error is formatted as the exception's message
use anyhow::{Context, Result};
fn file_exists(name: &str) -> binder::Result<bool> {
std::fs::metadata(name)
.context("file {} not found")
.or_service_specific_exception(NOT_FOUND)?
}
// General binder exceptions can be created similarly
fn file_exists(name: &str) -> binder::Result<bool> {
std::fs::metadata(name)
.or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?
}
Required Methods§
Sourcefn or_binder_exception(self, exception: ExceptionCode) -> Result<T, Status>
fn or_binder_exception(self, exception: ExceptionCode) -> Result<T, Status>
Converts the embedded error into a general binder exception of code exception
. The
message of the exception is set by formatting the error for debugging.
Sourcefn or_binder_exception_with<M: AsRef<str>, O: FnOnce(E) -> M>(
self,
exception: ExceptionCode,
op: O,
) -> Result<T, Status>
fn or_binder_exception_with<M: AsRef<str>, O: FnOnce(E) -> M>( self, exception: ExceptionCode, op: O, ) -> Result<T, Status>
Converts the embedded error into a general binder exception of code exception
. The
message of the exception is set by lazily evaluating the op
function.
Sourcefn or_service_specific_exception(self, error_code: i32) -> Result<T, Status>
fn or_service_specific_exception(self, error_code: i32) -> Result<T, Status>
Converts the embedded error into a service-specific binder exception. error_code
is used
to distinguish different service-specific binder exceptions. The message of the exception
is set by formatting the error for debugging.
Sourcefn or_service_specific_exception_with<M: AsRef<str>, O: FnOnce(E) -> M>(
self,
error_code: i32,
op: O,
) -> Result<T, Status>
fn or_service_specific_exception_with<M: AsRef<str>, O: FnOnce(E) -> M>( self, error_code: i32, op: O, ) -> Result<T, Status>
Converts the embedded error into a service-specific binder exception. error_code
is used
to distinguish different service-specific binder exceptions. The message of the exception
is set by lazily evaluating the op
function.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.