diff --git a/README.md b/README.md index 085a737..717fe6a 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ A simple example demonstrates its flexibility: ```rust use scroll::{ctx, Pread, LE}; -fn parse() -> Result<(), scroll::Error> { +fn main() -> Result<(), scroll::Error> { let bytes: [u8; 4] = [0xde, 0xad, 0xbe, 0xef]; // reads a u32 out of `b` with the endianness of the host machine, at offset 0, turbofish-style @@ -71,20 +71,13 @@ fn parse() -> Result<(), scroll::Error> { assert_eq!("world", world); Ok(()) } - -fn main() { - parse().unwrap(); -} ``` ### Deriving `Pread` and `Pwrite` Scroll implements a custom derive that can provide `Pread` and `Pwrite` implementations for your structs. -```no_test -#[macro_use] -extern crate scroll_derive; - +```rust use scroll::{Pread, Pwrite, BE}; #[derive(Pread, Pwrite)] @@ -94,7 +87,7 @@ struct Data { three: u8, } -fn parse() -> Result<(), scroll::Error> { +fn main() -> Result<(), scroll::Error> { let bytes: [u8; 7] = [0xde, 0xad, 0xbe, 0xef, 0xfa, 0xce, 0xff]; // Read a single `Data` at offset zero in big-endian byte order. let data: Data = bytes.pread_with(0, BE)?; @@ -108,10 +101,6 @@ fn parse() -> Result<(), scroll::Error> { assert_eq!(bytes, out); Ok(()) } - -fn main() { - parse().unwrap(); -} ``` This feature is **not** enabled by default, you must enable the `derive` feature in Cargo.toml to use it: @@ -129,7 +118,7 @@ Scroll can also read/write simple types from a `std::io::Read` or `std::io::Writ use std::io::Cursor; use scroll::IOread; -fn parse_io() -> Result<(), scroll::Error> { +fn main() -> Result<(), scroll::Error> { let bytes_ = [0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xef,0xbe,0x00,0x00,]; let mut bytes = Cursor::new(bytes_); @@ -139,10 +128,6 @@ fn parse_io() -> Result<(), scroll::Error> { let bar = bytes.ioread::()?; Ok(()) } - -fn main() { - parse_io().unwrap(); -} ``` Similarly, we can write to anything that implements `std::io::Write` quite naturally: @@ -151,7 +136,7 @@ Similarly, we can write to anything that implements `std::io::Write` quite natur use scroll::{IOwrite, LE, BE}; use std::io::{Write, Cursor}; -fn write_io() -> Result<(), scroll::Error> { +fn main() -> Result<(), scroll::Error> { let mut bytes = [0x0u8; 10]; let mut cursor = Cursor::new(&mut bytes[..]); cursor.write_all(b"hello")?; @@ -159,10 +144,6 @@ fn write_io() -> Result<(), scroll::Error> { assert_eq!(cursor.into_inner(), [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xde, 0xad, 0xbe, 0xef, 0x0]); Ok(()) } - -fn main() { - write_io().unwrap(); -} ``` # Advanced Uses @@ -196,17 +177,13 @@ impl<'a> ctx::TryFromCtx<'a, Endian> for Data<'a> { } } -fn parse_data() -> Result<(), scroll::Error> { +fn main() -> Result<(), scroll::Error> { let bytes = b"UserName\x00\x01\x02\x03\x04"; let data = bytes.pread_with::(0, BE)?; assert_eq!(data.id, 0x01020304); assert_eq!(data.name.to_string(), "UserName".to_string()); Ok(()) } - -fn main() { - parse_data().unwrap(); -} ``` Please see the official documentation, or a simple [example](examples/data_ctx.rs) for more. diff --git a/src/lib.rs b/src/lib.rs index f16f153..7aa943a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,6 +169,19 @@ pub mod export { pub use ::core::mem; } + +#[allow(unused)] +macro_rules! doc_comment { + ($x:expr) => { + #[doc = $x] + #[doc(hidden)] + mod readme_tests {} + }; +} + +#[cfg(feature = "derive")] +doc_comment!(include_str!("../README.md")); + #[cfg(test)] mod tests { #[allow(overflowing_literals)] diff --git a/tests/readme.rs b/tests/readme.rs deleted file mode 100644 index 60cde57..0000000 --- a/tests/readme.rs +++ /dev/null @@ -1,29 +0,0 @@ -use std::env; -use std::env::consts::EXE_EXTENSION; -use std::path::Path; -use std::process::Command; - -#[test] -fn readme_test() { - let rustdoc = Path::new("rustdoc").with_extension(EXE_EXTENSION); - let readme = Path::new(file!()).parent().unwrap().parent().unwrap().join("README.md"); - let exe = env::current_exe().unwrap(); - let depdir = exe.parent().unwrap(); - let outdir = depdir.parent().unwrap(); - let extern_arg = format!("scroll={}", outdir.join("libscroll.rlib").to_string_lossy()); - let mut cmd = Command::new(rustdoc); - cmd.args(&["--verbose", "--test", "-L"]) - .arg(&outdir) - .arg("-L") - .arg(&depdir) - .arg("--extern") - .arg(&extern_arg) - .arg(&readme); - println!("Running `{:?}`", cmd); - let result = cmd.spawn() - .expect("Failed to spawn process") - .wait() - .expect("Failed to run process"); - // fixme: i dont know why this is failing, so disabling - // assert!(result.success(), "Failed to run rustdoc tests on README.md!"); -}