summaryrefslogtreecommitdiff
path: root/subprojects/thiserror/tests/no-std/test.rs
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2025-11-15 20:18:36 +0000
committerJohn Turner <jturner.usa@gmail.com>2025-11-15 20:18:36 +0000
commitc0bd0c9efd429b35354484e24fe272fa1fbfe901 (patch)
tree395d4918e6a2b882fc05b285001b6cb72ab10af2 /subprojects/thiserror/tests/no-std/test.rs
parent0436fbc77039fa3d754f2db5cfefdd437fea51d1 (diff)
parent6eba9cd92c295c5389944f6adda1f1e83b2cb008 (diff)
downloadgentoo-utils-c0bd0c9efd429b35354484e24fe272fa1fbfe901.tar.gz
Merge commit '6eba9cd92c295c5389944f6adda1f1e83b2cb008' as 'subprojects/thiserror'
Diffstat (limited to 'subprojects/thiserror/tests/no-std/test.rs')
-rw-r--r--subprojects/thiserror/tests/no-std/test.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/subprojects/thiserror/tests/no-std/test.rs b/subprojects/thiserror/tests/no-std/test.rs
new file mode 100644
index 0000000..da7899c
--- /dev/null
+++ b/subprojects/thiserror/tests/no-std/test.rs
@@ -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 Write for Buf<'_> {
+ 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~~~");
+ }
+}