diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9ab88df..9a1e27f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,7 +13,7 @@ jobs: matrix: build: [MSRV, stable, nightly, macos, win32, win64] include: - - build: MSRV # Minimum supported Rust version + - build: MSRV # Minimum supported Rust version, ensure it matches the rust-version in Cargo.toml os: ubuntu-latest rust: 1.62.1 - build: stable @@ -56,3 +56,4 @@ jobs: cargo fmt -- --check - run: cargo build - run: cargo test + - run: cargo test --no-default-features diff --git a/src/ctx.rs b/src/ctx.rs index 1f982b8..555385a 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -267,6 +267,7 @@ pub trait FromCtx { /// `[u8]`), then you need to implement this trait /// /// ```rust +/// ##[cfg(feature = "std")] { /// use scroll::{self, ctx, Pread}; /// #[derive(Debug, PartialEq, Eq)] /// pub struct Foo(u16); @@ -286,6 +287,7 @@ pub trait FromCtx { /// /// let foo2 = bytes.pread_with::(0, scroll::BE).unwrap(); /// assert_eq!(Foo(0xdeadu16), foo2); +/// # } /// ``` /// /// # Advanced: Using Your Own Error in `TryFromCtx` @@ -350,6 +352,7 @@ pub trait IntoCtx: Sized { /// To implement writing into an arbitrary byte buffer, implement `TryIntoCtx` /// # Example /// ```rust +/// ##[cfg(feature = "std")] { /// use scroll::{self, ctx, LE, Endian, Pwrite}; /// #[derive(Debug, PartialEq, Eq)] /// pub struct Foo(u16); @@ -369,6 +372,7 @@ pub trait IntoCtx: Sized { /// /// let mut bytes: [u8; 4] = [0, 0, 0, 0]; /// bytes.pwrite_with(Foo(0x7f), 1, LE).unwrap(); +/// # } /// ``` pub trait TryIntoCtx: Sized { type Error; @@ -864,6 +868,7 @@ impl TryIntoCtx for CString { #[cfg(test)] mod tests { + #[cfg(feature = "std")] use super::*; #[test] diff --git a/src/error.rs b/src/error.rs index 7740254..8f92b85 100644 --- a/src/error.rs +++ b/src/error.rs @@ -23,6 +23,9 @@ pub enum Error { #[cfg(feature = "std")] /// A custom Scroll error for reporting messages to clients Custom(String), + #[cfg(not(feature = "std"))] + /// A custom static Scroll error for reporting messages to clients + Custom(&'static str), #[cfg(feature = "std")] /// Returned when IO based errors are encountered IO(io::Error), @@ -73,6 +76,10 @@ impl Display for Error { Error::Custom(ref msg) => { write!(fmt, "{}", msg) } + #[cfg(not(feature = "std"))] + Error::Custom(msg) => { + write!(fmt, "{msg}") + } #[cfg(feature = "std")] Error::IO(ref err) => { write!(fmt, "{}", err) diff --git a/src/leb128.rs b/src/leb128.rs index 43f50b9..52050d6 100644 --- a/src/leb128.rs +++ b/src/leb128.rs @@ -184,6 +184,7 @@ mod tests { let buf = [2u8 | CONTINUATION_BIT, 1]; let bytes = &buf[..]; let num = bytes.pread::(0).unwrap(); + #[cfg(feature = "std")] println!("num: {:?}", &num); assert_eq!(130u64, num.into()); assert_eq!(num.size(), 2); @@ -191,6 +192,7 @@ mod tests { let buf = [0x00, 0x01]; let bytes = &buf[..]; let num = bytes.pread::(0).unwrap(); + #[cfg(feature = "std")] println!("num: {:?}", &num); assert_eq!(0u64, num.into()); assert_eq!(num.size(), 1); @@ -198,6 +200,7 @@ mod tests { let buf = [0x21]; let bytes = &buf[..]; let num = bytes.pread::(0).unwrap(); + #[cfg(feature = "std")] println!("num: {:?}", &num); assert_eq!(0x21u64, num.into()); assert_eq!(num.size(), 1); diff --git a/src/lib.rs b/src/lib.rs index dcb58e7..26f232f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,6 +119,7 @@ //! [FromCtx](ctx/trait.FromCtx.html) and [SizeWith](ctx/trait.SizeWith.html). //! //! ```rust +//! ##[cfg(feature = "std")] { //! use std::io::Cursor; //! use scroll::{IOread, ctx, Endian}; //! let bytes = [0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xef,0xbe,0x00,0x00,]; @@ -139,12 +140,14 @@ //! // read/written, e.g. switching between ELF32 or ELF64 at runtime. //! let size = >::size_with(&Endian::Little) as u64; //! assert_eq!(prev + size, after); +//! # } //! ``` //! //! In the same vein as IOread we can use IOwrite to write a type to anything implementing //! `std::io::Write`: //! //! ```rust +//! ##[cfg(feature = "std")] { //! use std::io::Cursor; //! use scroll::{IOwrite}; //! @@ -155,6 +158,7 @@ //! cursor.iowrite_with(0xdeadbeef as u32, scroll::BE).unwrap(); //! //! assert_eq!(cursor.into_inner(), [0xde, 0xad, 0xbe, 0xef, 0x0]); +//! # } //! ``` //! //! ## Complex use cases @@ -355,18 +359,22 @@ mod tests { let bytes: [u8; 2] = [0x2e, 0x0]; let b = &bytes[..]; let s: &str = b.pread(0).unwrap(); + #[cfg(feature = "std")] println!("str: {}", s); assert_eq!(s.len(), bytes[..].len() - 1); let bytes: &[u8] = b"hello, world!\0some_other_things"; let hello_world: &str = bytes.pread_with(0, StrCtx::Delimiter(NULL)).unwrap(); + #[cfg(feature = "std")] println!("{:?}", &hello_world); assert_eq!(hello_world.len(), 13); let hello: &str = bytes.pread_with(0, StrCtx::Delimiter(SPACE)).unwrap(); + #[cfg(feature = "std")] println!("{:?}", &hello); assert_eq!(hello.len(), 6); // this could result in underflow so we just try it let _error = bytes.pread_with::<&str>(6, StrCtx::Delimiter(SPACE)); let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE)); + #[cfg(feature = "std")] println!("{:?}", &error); assert!(error.is_ok()); } @@ -377,13 +385,16 @@ mod tests { use super::Pread; let bytes: &[u8] = b""; let hello_world = bytes.pread_with::<&str>(0, StrCtx::Delimiter(NULL)); + #[cfg(feature = "std")] println!("1 {:?}", &hello_world); assert_eq!(hello_world.is_err(), true); let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE)); + #[cfg(feature = "std")] println!("2 {:?}", &error); assert!(error.is_err()); let bytes: &[u8] = b"\0"; let null = bytes.pread::<&str>(0).unwrap(); + #[cfg(feature = "std")] println!("3 {:?}", &null); assert_eq!(null.len(), 0); } @@ -425,7 +436,8 @@ mod tests { } } - impl error::Error for ExternalError { + #[cfg(feature = "std")] + impl std::error::Error for ExternalError { fn description(&self) -> &str { "ExternalError" } @@ -499,7 +511,7 @@ mod tests { let mut offset = 0; let deadbeef: $typ = bytes.gread_with(&mut offset, LE).unwrap(); assert_eq!(deadbeef, $deadbeef as $typ); - assert_eq!(offset, ::std::mem::size_of::<$typ>()); + assert_eq!(offset, ::core::mem::size_of::<$typ>()); } }; } @@ -518,7 +530,7 @@ mod tests { let mut offset = 0; let deadbeef: $typ = bytes.gread_with(&mut offset, LE).unwrap(); assert_eq!(deadbeef, $deadbeef as $typ); - assert_eq!(offset, ::std::mem::size_of::<$typ>()); + assert_eq!(offset, ::core::mem::size_of::<$typ>()); } }; } @@ -537,8 +549,8 @@ mod tests { let o2 = &mut 0; let val: $typ = buffer.gread_with(o2, LE).unwrap(); assert_eq!(val, $val); - assert_eq!(*offset, ::std::mem::size_of::<$typ>()); - assert_eq!(*o2, ::std::mem::size_of::<$typ>()); + assert_eq!(*offset, ::core::mem::size_of::<$typ>()); + assert_eq!(*o2, ::core::mem::size_of::<$typ>()); assert_eq!(*o2, *offset); buffer.gwrite_with($val.clone(), offset, BE).unwrap(); let val: $typ = buffer.gread_with(o2, BE).unwrap(); @@ -612,12 +624,18 @@ mod tests { let res = b.gread_with::<&str>(offset, StrCtx::Length(3)); assert!(res.is_err()); *offset = 0; - let astring: [u8; 3] = [0x45, 042, 0x44]; + // FIXME: 0x2A used to be 042 -- a weird way to declare a number, probably a bug + let astring: [u8; 3] = [0x45, 0x2A, 0x44]; let string = astring.gread_with::<&str>(offset, StrCtx::Length(2)); match &string { - &Ok(_) => {} - &Err(ref err) => { - println!("{}", &err); + Ok(_) => {} + #[cfg(feature = "std")] + Err(err) => { + println!("{err}"); + panic!(); + } + #[cfg(not(feature = "std"))] + Err(_) => { panic!(); } } diff --git a/tests/api.rs b/tests/api.rs index e10726f..8014287 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -193,6 +193,7 @@ impl scroll::ctx::SizeWith for Foo { } #[test] +#[cfg(feature = "std")] fn ioread_api() { use scroll::{IOread, LE}; use std::io::Cursor;