hex-slice-0.1.4/.gitignore010064400007650000024000000000221324634544000136030ustar0000000000000000target Cargo.lock hex-slice-0.1.4/.travis.yml010064400007650000024000000012331324634544000137310ustar0000000000000000language: rust sudo: false language: rust rust: - stable - beta - nightly cache: directories: - $HOME/.cargo - target install: - true before_script: - | pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH env: - TRAVIS_CARGO_NIGHTLY_FEATURE="" script: - travis-cargo build -- $EXTRA - travis-cargo test -- $EXTRA - travis-cargo --only stable doc after_success: # upload the documentation from the build with stable (automatically only actually # runs on the master branch, not individual PRs) - travis-cargo --only stable doc-upload matrix: allow_failures: - rust: nightly - rust: beta hex-slice-0.1.4/Cargo.toml.orig010064400007650000024000000005541324635062300145130ustar0000000000000000[package] name = "hex-slice" version = "0.1.4" description = "Extends the std::fmt::*Hex traits to slices" authors = ["Ceri Storey "] documentation = "https://cstorey.github.io/hex-slice/hex_slice/index.html" license = "MIT" readme = "README.md" repository = "https://github.com/cstorey/hex-slice/" [dev-dependencies] suppositions = "0.1.2" hex-slice-0.1.4/Cargo.toml0000644000000015770000000000000107660ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "hex-slice" version = "0.1.4" authors = ["Ceri Storey "] description = "Extends the std::fmt::*Hex traits to slices" documentation = "https://cstorey.github.io/hex-slice/hex_slice/index.html" readme = "README.md" license = "MIT" repository = "https://github.com/cstorey/hex-slice/" [dev-dependencies.suppositions] version = "0.1.2" hex-slice-0.1.4/examples/trivial.rs010064400007650000024000000002051324634544000154540ustar0000000000000000extern crate hex_slice; use hex_slice::AsHex; fn main() { let foo = vec![0u32, 1 ,2 ,3]; println!("{:x}", foo.as_hex()); } hex-slice-0.1.4/README.md010064400007650000024000000011311324635061000130670ustar0000000000000000# hex-slice [![Build Status](https://travis-ci.org/cstorey/hex-slice.svg?branch=master)](https://travis-ci.org/cstorey/hex-slice) Renders a slice of integers (or anything else that supports the [LowerHex](https://doc.rust-lang.org/std/fmt/trait.LowerHex.html) or [UpperHex](https://doc.rust-lang.org/std/fmt/trait.UpperHex.html) traits) as hex. For example, this: ```rust extern crate hex_slice; use hex_slice::AsHex; fn main() { let foo = vec![0u32, 1 ,2 ,3]; println!("{:x}", foo.as_hex()); } ``` Displays: `[0 1 2 3]` on standard output (available with `cargo run --example trivial`). hex-slice-0.1.4/src/lib.rs010064400007650000024000000043531324635000300135200ustar0000000000000000//! The purpose of this crate is to extend the `UpperHex` and `LowerHex` //! traits to slices, as well as the integers it is currently implemented for. //! //! # Examples //! //! ```rust //! extern crate hex_slice; //! use hex_slice::AsHex; //! //! fn main() { //! let foo = vec![0u32, 1 ,2 ,3]; //! println!("{:x}", foo.as_hex()); //! } //! ``` #![no_std] use core::fmt; use core::fmt::Write; pub struct Hex<'a, T: 'a>(&'a [T]); pub struct PlainHex<'a, T: 'a> { slice: &'a [T], with_spaces: bool, } pub trait AsHex { type Item; fn as_hex<'a>(&'a self) -> Hex<'a, Self::Item>; fn plain_hex<'a>(&'a self, with_spaces: bool) -> PlainHex<'a, Self::Item>; } fn fmt_inner_hex fmt::Result>(slice: &[T], f: &mut fmt::Formatter, fmt_fn: F, with_spaces: bool) -> fmt::Result { for (i, val) in slice.iter().enumerate() { if with_spaces && i > 0 { f.write_char(' ')?; } fmt_fn(val, f)?; } Ok(()) } impl<'a, T> Hex<'a, T> { pub fn hex(slice: &'a [T]) -> Hex<'a, T> { Hex(slice) } } impl<'a, T: fmt::LowerHex> fmt::LowerHex for Hex<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[")?; fmt_inner_hex(self.0, f, fmt::LowerHex::fmt, true)?; write!(f, "]") } } impl<'a, T: fmt::UpperHex> fmt::UpperHex for Hex<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[")?; fmt_inner_hex(self.0, f, fmt::UpperHex::fmt, true)?; write!(f, "]") } } impl<'a, T: fmt::LowerHex> fmt::LowerHex for PlainHex<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt_inner_hex(self.slice, f, fmt::LowerHex::fmt, self.with_spaces) } } impl<'a, T: fmt::UpperHex> fmt::UpperHex for PlainHex<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt_inner_hex(self.slice, f, fmt::UpperHex::fmt, self.with_spaces) } } impl AsHex for [T] { type Item = T; fn as_hex<'a>(&'a self) -> Hex<'a, Self::Item> { Hex::hex(self) } fn plain_hex<'a>(&'a self, with_spaces: bool) -> PlainHex<'a, Self::Item> { PlainHex { slice: self, with_spaces, } } } hex-slice-0.1.4/tests/properties.rs010064400007650000024000000056211324635000300155200ustar0000000000000000extern crate suppositions; extern crate hex_slice; use hex_slice::AsHex; use suppositions::*; use suppositions::generators::*; #[test] fn should_start_and_end_with_square_brackets() { property(vecs(u8s())) .check(|data| { let s = format!("{:x}", data.as_hex()); let len = s.len(); let first = s.get(..1); let last = s.get(len-1..); first == Some("[") && last == Some("]") }); } #[test] fn should_group_u8_as_blocks_of_two() { property(vecs(u8s()).map(|data| (data.clone(), format!("{:02x}", data.as_hex())))) .check(|(_, s)| { let len = s.len(); let body = match s.get(1..len-1) { Some(slice) => slice, // Should always have a middle. None => return false, }; body.split_whitespace().all(|chunk| chunk.len() == 2) }); } #[test] fn should_group_u32_as_blocks_of_eight_when_specified() { property(vecs(u32s()).map(|data| (data.clone(), format!("{:08x}", data.as_hex())))) .check(|(_, s)| { let len = s.len(); let body = match s.get(1..len-1) { Some(slice) => slice, // Should always have a middle. None => return false, }; body.split_whitespace().all(|chunk| chunk.len() == 8) }); } #[test] fn should_be_decodable_as_original_data() { property(vecs(u32s()).map(|data| (data.clone(), format!("{:x}", data.as_hex())))) .check(|(orig, s)| -> std::result::Result { let len = s.len(); let body = match s.get(1..len-1) { Some(slice) => slice, // Should always have a middle. None => return Ok(false), }; body.split_whitespace() .map(|chunk| u32::from_str_radix(chunk, 16)) .collect::, _>>() .map(|d| d == orig) }); } #[test] fn plain_hex_should_group_u8_as_blocks_of_two() { property(vecs(u8s()).map(|data| (data.clone(), format!("{:02x}", data.plain_hex(true))))) .check(|(_, s)| { s.split_whitespace().all(|chunk| chunk.len() == 2) }); } #[test] fn plain_hex_should_group_u32_as_blocks_of_eight_when_specified() { property(vecs(u32s()).map(|data| (data.clone(), format!("{:08x}", data.plain_hex(true))))) .check(|(_, s)| { s.split_whitespace().all(|chunk| chunk.len() == 8) }); } #[test] fn plain_hex_should_be_decodable_as_original_data() { property(vecs(u32s()).map(|data| (data.clone(), format!("{:x}", data.plain_hex(true))))) .check(|(orig, s)| -> std::result::Result { s.split_whitespace() .map(|chunk| u32::from_str_radix(chunk, 16)) .collect::, _>>() .map(|d| d == orig) }); }