Skip to content

Commit

Permalink
Merge pull request #139 from lvgl/error-impl
Browse files Browse the repository at this point in the history
Implement the `Error` trait for our internal error types
  • Loading branch information
nia-e authored Jun 20, 2023
2 parents 5acb438 + dccfffc commit 216d7ab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lvgl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ use-vendored-config = ["lvgl-sys/use-vendored-config"]
# on the timer module for usage notes.
rust_timer = ["lvgl-sys/rust_timer"]

# Enables some unstable features. Currently, only #[cfg_accessible] is used.
# Enables some unstable features. Currently, #![feature(cfg_accessible)] and
# #![feature(error_in_core)] are used.
# This feature will currently allow:
# - Using built-in LVGL fonts other than the default
# - Handling LvErrors/LvResults with error-handling libraries i.e. anyhow
nightly = []

# Disables auto-initializing LVGL.
Expand Down
16 changes: 16 additions & 0 deletions lvgl/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use core::mem::{ManuallyDrop, MaybeUninit};
use core::pin::Pin;
use core::ptr::NonNull;
use core::{ptr, result};
use core::fmt;
#[cfg(feature = "nightly")]
use core::error::Error;

/// Error in interacting with a `Display`.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand All @@ -16,6 +19,19 @@ pub enum DisplayError {
NotRegistered,
}

impl fmt::Display for DisplayError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Display {}", match self {
DisplayError::NotAvailable => "not available",
DisplayError::FailedToRegister => "failed to register",
DisplayError::NotRegistered => "not registered",
})
}
}

#[cfg(feature = "nightly")]
impl Error for DisplayError {}

type Result<T> = result::Result<T, DisplayError>;

/// An LVGL-registered display. Equivalent to an `lv_disp_t`.
Expand Down
1 change: 1 addition & 0 deletions lvgl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "nightly", feature(cfg_accessible))]
#![cfg_attr(feature = "nightly", feature(error_in_core))]

#[macro_use]
extern crate bitflags;
Expand Down
18 changes: 17 additions & 1 deletion lvgl/src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::display::DisplayError;
use crate::Widget;
use core::convert::{TryFrom, TryInto};
use core::ptr::NonNull;

use core::fmt;
#[cfg(feature = "nightly")]
use core::error::Error;
#[cfg(feature = "embedded_graphics")]
use embedded_graphics::pixelcolor::{Rgb565, Rgb888};

Expand All @@ -17,6 +19,20 @@ pub enum LvError {
AlreadyInUse,
}

impl fmt::Display for LvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", match self {
LvError::InvalidReference => "Accessed invalid reference or ptr",
LvError::Uninitialized => "LVGL uninitialized",
LvError::LvOOMemory => "LVGL out of memory",
LvError::AlreadyInUse => "Resource already in use",
})
}
}

#[cfg(feature = "nightly")]
impl Error for LvError {}

impl From<DisplayError> for LvError {
fn from(err: DisplayError) -> Self {
use LvError::*;
Expand Down

0 comments on commit 216d7ab

Please sign in to comment.