From eb93ed0b575a612af02e14b660e30486c27b9863 Mon Sep 17 00:00:00 2001 From: pommicket Date: Mon, 8 Sep 2025 12:03:54 -0400 Subject: Fix no_std build --- pre-commit.sh | 3 +++ src/lib.rs | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pre-commit.sh b/pre-commit.sh index bcffdca..603bc0f 100755 --- a/pre-commit.sh +++ b/pre-commit.sh @@ -1,4 +1,7 @@ #!/bin/sh cargo fmt || exit 1 +cargo check || exit 1 cargo clippy -- -D warnings || exit 1 +# Check that there are no errors with no_std +cargo check --target=wasm32-unknown-unknown --no-default-features || exit 1 git add -u || exit 1 diff --git a/src/lib.rs b/src/lib.rs index 935fa96..40a2322 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,12 @@ #![warn(clippy::redundant_closure_for_method_calls)] extern crate alloc; +use alloc::borrow::ToOwned; +use alloc::boxed::Box; +use alloc::string::String; use alloc::sync::Arc; use alloc::vec::Vec; +use alloc::{format, vec}; use core::fmt; use core::mem::take; @@ -117,6 +121,7 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + #[cfg(feature = "std")] Self::IO(message, io_err) => write!(f, "{message}: {io_err}"), Self::IllegalCharacter(location, c) => { write!(f, "{location}: illegal character {c:?}") @@ -174,17 +179,17 @@ impl fmt::Display for Error { } impl core::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { + #[cfg(feature = "std")] if let Error::IO(_, e) = self { - Some(e) - } else { - None + return Some(e); } + None } } /// Type alias for [`std::result::Result`] with [`Error`] as the error. -pub type Result = std::result::Result; +pub type Result = core::result::Result; fn parse_int(location: &Location, string: &str) -> Result { let bad_int = || Error::BadInt(location.clone(), string.into()); -- cgit v1.2.3