-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow disabling std dependency on 1.81+
- Loading branch information
Showing
10 changed files
with
148 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "thiserror_test" | ||
version = "0.0.0" | ||
authors = ["David Tolnay <[email protected]>"] | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
path = "test.rs" | ||
|
||
[dependencies] | ||
thiserror = { path = "../..", default-features = false } | ||
|
||
[features] | ||
default = ["std"] | ||
std = ["thiserror/std"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#![no_std] | ||
|
||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Error::E")] | ||
E(#[from] SourceError), | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
#[error("SourceError {field}")] | ||
pub struct SourceError { | ||
pub field: i32, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{Error, SourceError}; | ||
use core::error::Error as _; | ||
use core::fmt::{self, Write}; | ||
use core::mem; | ||
|
||
struct Buf<'a>(&'a mut [u8]); | ||
|
||
impl<'a> Write for Buf<'a> { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
if s.len() <= self.0.len() { | ||
let (out, rest) = mem::take(&mut self.0).split_at_mut(s.len()); | ||
out.copy_from_slice(s.as_bytes()); | ||
self.0 = rest; | ||
Ok(()) | ||
} else { | ||
Err(fmt::Error) | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let source = SourceError { field: -1 }; | ||
let error = Error::from(source); | ||
|
||
let source = error | ||
.source() | ||
.unwrap() | ||
.downcast_ref::<SourceError>() | ||
.unwrap(); | ||
|
||
let mut msg = [b'~'; 17]; | ||
write!(Buf(&mut msg), "{}", error).unwrap(); | ||
assert_eq!(msg, *b"Error::E~~~~~~~~~"); | ||
|
||
let mut msg = [b'~'; 17]; | ||
write!(Buf(&mut msg), "{}", source).unwrap(); | ||
assert_eq!(msg, *b"SourceError -1~~~"); | ||
} | ||
} |