Skip to content

Commit

Permalink
Update error and example
Browse files Browse the repository at this point in the history
  • Loading branch information
bsodmike committed Feb 29, 2024
1 parent 6f6ae3d commit 8888b56
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ embedded-hal = { package = "embedded-hal", version = "^1.0" }
embedded-hal-0-2 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"] }
serde = { version = "1", features = ["derive"], default-features = false, optional = true }
embedded-hal-async = { package = "embedded-hal-async", version = "^1.0", optional = true }
linux-embedded-hal = "0.3.2"

[dev-dependencies]
i2cdev = "0.5.1"
linux-embedded-hal = "0.3.0"


[package.metadata.docs.rs]
all-features = true
Expand Down
9 changes: 4 additions & 5 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! This example reads the chip ID from a RV8803.
use i2cdev::linux::LinuxI2CError;
use linux_embedded_hal::I2cdev;
use rv8803::models::{Rv8803, Rv8803Error, TIME_ARRAY_LENGTH};
use rv8803::models::{CrateError, Rv8803, TIME_ARRAY_LENGTH};

fn main() -> Result<(), Rv8803Error<LinuxI2CError>> {
let i2c = I2cdev::new("/dev/i2c-1").map_err(Rv8803Error::I2c)?;
fn main() -> Result<(), CrateError> {
let dev = I2cdev::new("/dev/i2c-1");
let i2c = dev.map_err(CrateError::default())?;

let mut rtc: Rv8803<_> =
Rv8803::from_i2c(i2c, rv8803::bus::Address::Default).expect("Failed to initialize RV8803");
Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly-2024-02-22"
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#![cfg_attr(docsrs, feature(doc_cfg), feature(doc_auto_cfg))]
#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![feature(error_in_core)]
#![feature(error_generic_member_access)]
#![feature(fn_traits)]
#![feature(unboxed_closures)]

use crate::bus::{Bus, BusTrait};
pub use embedded_hal_0_2;
Expand Down
66 changes: 63 additions & 3 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,69 @@
/// All possible errors in this crate
use alloc::boxed::Box;
use core::{error, fmt::Display};

/// Type for all crate errors
pub type CrateError = Error;
/// Boxed error type
pub type BoxError = Box<dyn error::Error + Send + Sync>;

#[allow(dead_code)]
#[derive(Debug)]
pub enum Rv8803Error<E> {
/// I2C bus error
I2c(E),
/// Error struct
pub struct Error {
inner: BoxError,
}

impl Error {
/// Create a new instance of [`Error`]
pub fn new(error: impl Into<BoxError>) -> Self {
Self {
inner: error.into(),
}
}

/// Create a default instance of [`Error`]
#[allow(clippy::should_implement_trait)]
#[allow(clippy::unnecessary_literal_unwrap)]
pub fn default() -> Self {
Self {
// FIXME: isn't this the same as `panic!("{:?}", ())`??
inner: Err(()).unwrap(),
}
}
}

impl FnOnce<(linux_embedded_hal::i2cdev::linux::LinuxI2CError,)> for Error {
type Output = Error;

extern "rust-call" fn call_once(
self,
args: (linux_embedded_hal::i2cdev::linux::LinuxI2CError,),
) -> Self::Output {
Error::new(args.0)
}
}

impl Display for CrateError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.inner.fmt(f)
}
}

impl error::Error for CrateError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(&*self.inner)
}

fn description(&self) -> &str {
"description() is deprecated; use Display"
}

fn cause(&self) -> Option<&dyn error::Error> {
self.source()
}

fn provide<'a>(&'a self, _request: &mut core::error::Request<'a>) {}
}

/// Mapping of all the registers used to operate the RTC module
Expand Down

0 comments on commit 8888b56

Please sign in to comment.