Skip to content

Commit 0b61975

Browse files
authored
Implement std::error::Error (#71)
1 parent d44a287 commit 0b61975

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- name: Install thumbv7em-none-eabihf target
4848
run: rustup target add thumbv7em-none-eabihf
4949

50-
- name: Run cargo build
50+
- name: Run cargo build (no_std)
5151
uses: actions-rs/cargo@v1
5252
env:
5353
RUSTFLAGS: -D warnings
@@ -61,7 +61,7 @@ jobs:
6161
RUSTFLAGS: -D warnings
6262
with:
6363
command: build
64-
args: --all-features --release --target thumbv7em-none-eabihf
64+
args: --all-features --release
6565

6666
test:
6767
name: Test Suite

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ features = ["vector"]
2525
[features]
2626
default = ["orientation"]
2727
orientation = []
28+
std = []

src/error.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,25 @@ where
100100
Self::new_with_cause(ErrorKind::Bus, cause)
101101
}
102102
}
103+
104+
#[cfg(feature = "std")]
105+
impl<E> std::error::Error for Error<E>
106+
where
107+
E: Debug + std::error::Error + 'static,
108+
{
109+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
110+
self.cause
111+
.as_ref()
112+
.map(|cause| cause as &(dyn std::error::Error + 'static))
113+
}
114+
}
115+
116+
#[cfg(feature = "std")]
117+
impl<E> Display for Error<E>
118+
where
119+
E: Debug,
120+
{
121+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122+
write!(f, "{}", self.kind)
123+
}
124+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#![forbid(unsafe_code)]
4040
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
4141

42+
#[cfg(feature = "std")]
43+
extern crate std;
44+
4245
mod accelerometer;
4346
pub mod error;
4447
#[cfg(feature = "orientation")]

0 commit comments

Comments
 (0)