Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support no_std #20

Merged
merged 5 commits into from Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,5 +5,6 @@ Cargo.lock
# Misc stuff
.*
!.gitignore
!.travis.yml
!.gitlab-ci.yml
!.gitlab-ci-matrix.yml
1 change: 1 addition & 0 deletions .gitlab-ci-matrix.yml
Expand Up @@ -21,6 +21,7 @@ steps:
stage: test
script:
- cargo test
- cargo test --no-default-features
dependencies:
- compile

Expand Down
6 changes: 6 additions & 0 deletions .gitlab-ci.yml
Expand Up @@ -113,6 +113,7 @@ test:rust-stable:
- compile:rust-stable
script:
- cargo test
- cargo test --no-default-features
variables:
RUST_KEY: rust-stable

Expand All @@ -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

Expand All @@ -133,6 +135,7 @@ test:rust-beta:
- compile:rust-beta
script:
- cargo test
- cargo test --no-default-features
variables:
RUST_KEY: rust-beta

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions .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
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -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 = []
20 changes: 12 additions & 8 deletions 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.
Expand All @@ -25,9 +26,11 @@
//! }
//! ```

extern crate core;
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

use std::error;
use core::fmt;
use core::iter;

Expand Down Expand Up @@ -55,8 +58,8 @@ pub trait ToHex {
fn encode_hex_upper<T: iter::FromIterator<char>>(&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>,
Expand All @@ -68,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,
}
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -347,7 +351,7 @@ pub fn decode_to_slice<T: AsRef<[u8]>>(data: T, out: &mut [u8]) -> Result<(), Fr

#[cfg(test)]
mod test {
use super::{encode, decode, FromHex, FromHexError};
use super::*;

#[test]
fn test_encode() {
Expand All @@ -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]
Expand Down