diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2973239 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,33 @@ +sudo: false +language: rust +rust: + - 1.33.0 # Oldest supported + - stable + - beta + - nightly +matrix: + include: + - env: RUSTFMT + rust: 1.33.0 # `stable`: Locking down for consistent behavior + install: + - rustup component add rustfmt-preview + script: + - cargo fmt -- --check + - env: RUSTFLAGS="-D warnings" + rust: 1.33.0 # `stable`: Locking down for consistent behavior + script: + - cargo check --tests + allow_failures: + - rust: nightly + fast_finish: true + +install: +- rustc -Vv +- cargo -V + +script: +- rm -rf target/debug/deps/*memoffset* # Avoid rustdoc problems +- cargo test --verbose + +cache: + cargo: true diff --git a/src/lib.rs b/src/lib.rs index e6d7487..0fa02c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ //! //! Some of the funcationality of the crate makes no sense when used along with structs that //! are not `#[repr(C, packed)]`, but it is up to the user to make sure that they are. -//! +//! //! This functionality should work for `const`s but presently doesn't work on `const fn`. Storing a //! value in a const and then returning it from a `const fn` should workaround most cases. //! @@ -67,9 +67,11 @@ #[doc(hidden)] pub use core::mem; -// A helper to get a const fn version of size_of_val +// A helper to get a const fn version of size_of_val #[doc(hidden)] -pub const fn size_of(_: &T) -> usize { mem::size_of::() } +pub const fn size_of(_: &T) -> usize { + mem::size_of::() +} // While constant pointer transmutation isn't stable, union transmutation is // This hack should go away after rust-lang/rust#51910 @@ -79,7 +81,6 @@ pub union Transmuter { pub int: usize, } - #[macro_use] mod offset_of; #[macro_use] diff --git a/src/span_of.rs b/src/span_of.rs index b44a8c0..fd61a7f 100644 --- a/src/span_of.rs +++ b/src/span_of.rs @@ -43,11 +43,11 @@ /// span_of!(Struct, start ..) /// ``` /// -/// *Note*: +/// *Note*: /// This macro uses recursion in order to resolve the range expressions, so there is a limit to the complexity of the expression. /// In order to raise the limit, the compiler's recursion limit should be lifted. /// -/// *Note*: +/// *Note*: /// This macro may not make much sense when used on structs that are not `#[repr(C, packed)]` /// /// ## Examples @@ -77,7 +77,7 @@ /// assert_eq!(0..42, span_of!(Blarg, x .. y[34])); /// assert_eq!(0..64, span_of!(Blarg, x ..= y)); /// assert_eq!(58..68, span_of!(Blarg, y[50] ..= z)); -/// +/// /// const SPAN: std::ops::Range = span_of!(Blarg, y[50] ..= z); /// assert_eq!(58..68, SPAN); /// } @@ -90,7 +90,7 @@ macro_rules! span_of { (@helper $root:ident, [] ..) => ( compile_error!("Expected a range, found '..'") ); - (@helper $root:ident, [] ..= $($field:tt)+) => ( + (@helper $root:ident, [] ..= $($field:tt)+) => ( 0..($crate::Transmuter { ptr: &$root.$($field)+ }.int + $crate::size_of(&$root.$($field)+)) ); (@helper $root:ident, [] .. $($field:tt)+) => ( @@ -120,7 +120,7 @@ macro_rules! span_of { span_of!(@helper $root, #$tt [] $($rest)*) }; - ($parent:ty, $($exp:tt)+) => (unsafe { + ($parent:ty, $($exp:tt)+) => (unsafe { let root: &'static $parent = $crate::Transmuter::<$parent> { int: 0 }.ptr; span_of!(@helper root, [] $($exp)*) }); @@ -128,7 +128,7 @@ macro_rules! span_of { #[cfg(test)] mod tests { - use ::core::mem; + use core::mem; #[repr(C, packed)] struct Foo { @@ -171,8 +171,8 @@ mod tests { assert_eq!(8..64, span_of!(Blarg, y[0]..z)); assert_eq!(0..42, span_of!(Blarg, x..y[34])); - assert_eq!(0..64, span_of!(Blarg, x ..= y)); - assert_eq!(58..68, span_of!(Blarg, y[50] ..= z)); + assert_eq!(0..64, span_of!(Blarg, x..=y)); + assert_eq!(58..68, span_of!(Blarg, y[50]..=z)); } #[test]