Add IntoBinderResult trait to simplify binder error handling
Before this change, returning binder::Result<T> was a bit cumbersome,
and the integration with anyhow was not nice:
```
use anyhow::{Context, Result};
fn file_exists(name: &str) -> binder::Result<bool> {
std::fs::metadata(name)
.with_context("cannot find {}")
.map_err(|e| {
Status::new_service_specific_err_str(
NOT_FOUND,
Some("{:?}", e)
)
})?
}
```
With this change, above can be simplified as below:
```
use binder::IntoBinderResult;
use anyhow::{Context, Result};
fn file_exists(name: &str) -> binder::Result<bool> {
std::fs::metadata(name)
.with_context("cannot find {}", name)
.or_service_specific_exception(NOT_FOUND)?
}
```
Bug: 294348831
Test: atest libbinder_rs-internal_test
Change-Id: I4c669ac39c01f648f68ecf6db7e088edec034825
Loading
Please register or sign in to comment