From ee1f74160ed81ba2b7f221919d113db52ccda113 Mon Sep 17 00:00:00 2001 From: koushiro Date: Thu, 11 Jul 2019 10:17:25 +0800 Subject: [PATCH 1/4] Add no_std support Signed-off-by: koushiro --- Cargo.toml | 3 +++ src/lib.rs | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e51a424..8ab26b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,9 @@ description = "Encoding and decoding data into/from hexadecimal representation." license = "MIT OR Apache-2.0" documentation = "https://docs.rs/hex/" repository = "https://github.com/KokaKiwi/rust-hex" +edition = "2018" [features] +default = ["std"] +std = [] benchmarks = [] diff --git a/src/lib.rs b/src/lib.rs index b7a7dc3..d7fa2f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![cfg_attr(feature = "benchmarks", feature(test))] +#![cfg_attr(not(feature = "std"), no_std)] // Copyright (c) 2013-2014 The Rust Project Developers. // Copyright (c) 2015-2018 The rust-hex Developers. @@ -25,9 +26,11 @@ //! } //! ``` -extern crate core; +#[cfg(not(feature = "std"))] +extern crate alloc; +#[cfg(not(feature = "std"))] +use alloc::{vec::Vec, string::String}; -use std::error; use core::fmt; use core::iter; @@ -142,7 +145,8 @@ pub enum FromHexError { InvalidStringLength, } -impl error::Error for FromHexError { +#[cfg(feature = "std")] +impl std::error::Error for FromHexError { fn description(&self) -> &str { match *self { FromHexError::InvalidHexCharacter { .. } => "invalid character", @@ -347,7 +351,7 @@ pub fn decode_to_slice>(data: T, out: &mut [u8]) -> Result<(), Fr #[cfg(test)] mod test { - use super::{encode, decode, FromHex, FromHexError}; + use super::*; #[test] fn test_encode() { @@ -356,7 +360,7 @@ mod test { #[test] fn test_decode() { - assert_eq!(decode("666f6f626172"), Ok("foobar".to_owned().into_bytes())); + assert_eq!(decode("666f6f626172"), Ok(String::from("foobar").into_bytes())); } #[test] From 0adc0ac8011ebb2086ec03aac21f6c479ee82460 Mon Sep 17 00:00:00 2001 From: koushiro Date: Fri, 12 Jul 2019 15:41:04 +0800 Subject: [PATCH 2/4] Migrate to 2018 edition Signed-off-by: koushiro --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dc6d346..a31865d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ #[cfg(not(feature = "std"))] extern crate alloc; #[cfg(not(feature = "std"))] -use alloc::{vec::Vec, string::String}; +use alloc::{string::String, vec::Vec}; use core::fmt; use core::iter; @@ -58,8 +58,8 @@ pub trait ToHex { fn encode_hex_upper>(&self) -> T; } -const HEX_CHARS_LOWER: &'static[u8; 16] = b"0123456789abcdef"; -const HEX_CHARS_UPPER: &'static[u8; 16] = b"0123456789ABCDEF"; +const HEX_CHARS_LOWER: &[u8; 16] = b"0123456789abcdef"; +const HEX_CHARS_UPPER: &[u8; 16] = b"0123456789ABCDEF"; struct BytesToHexChars<'a> { inner: ::core::slice::Iter<'a, u8>, @@ -71,7 +71,7 @@ impl<'a> BytesToHexChars<'a> { fn new(inner: &'a [u8], table: &'static [u8; 16]) -> BytesToHexChars<'a> { BytesToHexChars { inner: inner.iter(), - table: table, + table, next: None, } } From a7ea5fc357594581fc5f0b8c039b6f90d6b042e8 Mon Sep 17 00:00:00 2001 From: koushiro Date: Fri, 12 Jul 2019 16:15:20 +0800 Subject: [PATCH 3/4] Add no_std test for ci Signed-off-by: koushiro --- .gitlab-ci-matrix.yml | 1 + .gitlab-ci.yml | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/.gitlab-ci-matrix.yml b/.gitlab-ci-matrix.yml index 9c41771..fd08f03 100644 --- a/.gitlab-ci-matrix.yml +++ b/.gitlab-ci-matrix.yml @@ -21,6 +21,7 @@ steps: stage: test script: - cargo test + - cargo test --no-default-features dependencies: - compile diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bb02bc6..35c29a7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -113,6 +113,7 @@ test:rust-stable: - compile:rust-stable script: - cargo test + - cargo test --no-default-features variables: RUST_KEY: rust-stable @@ -123,6 +124,7 @@ test:rust-stable-musl: - compile:rust-stable-musl script: - cargo test + - cargo test --no-default-features variables: RUST_KEY: rust-stable-musl @@ -133,6 +135,7 @@ test:rust-beta: - compile:rust-beta script: - cargo test + - cargo test --no-default-features variables: RUST_KEY: rust-beta @@ -143,6 +146,7 @@ test:rust-beta-musl: - compile:rust-beta-musl script: - cargo test + - cargo test --no-default-features variables: RUST_KEY: rust-beta-musl @@ -153,6 +157,7 @@ test:rust-nightly: - compile:rust-nightly script: - cargo test + - cargo test --no-default-features variables: RUST_KEY: rust-nightly allow_failure: true @@ -164,6 +169,7 @@ test:rust-nightly-musl: - compile:rust-nightly-musl script: - cargo test + - cargo test --no-default-features variables: RUST_KEY: rust-nightly-musl allow_failure: true From 227a1e6c4c4d30d2efcddabb73e7030d0760f003 Mon Sep 17 00:00:00 2001 From: koushiro Date: Fri, 12 Jul 2019 16:36:09 +0800 Subject: [PATCH 4/4] Config travis ci Signed-off-by: koushiro --- .gitignore | 1 + .travis.yml | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/.gitignore b/.gitignore index 5d08807..941ffc5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ Cargo.lock # Misc stuff .* !.gitignore +!.travis.yml !.gitlab-ci.yml !.gitlab-ci-matrix.yml diff --git a/.travis.yml b/.travis.yml index 8c91a74..2d5c565 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,20 @@ language: rust +os: + - linux + - windows + - osx rust: - stable - beta - nightly +env: + global: + - RUST_BACKTRACE=1 + - RUSTFLAGS="-D warnings" matrix: allow_failures: - rust: nightly + +script: + - cargo test + - cargo test --no-default-features