Skip to main content

Crate hex_conservative

Crate hex_conservative 

Source
Expand description

§Hex encoding and decoding

General purpose hex encoding/decoding library with a conservative MSRV and dependency policy.

§Const hex literals

use hex_conservative::hex;

const GENESIS: [u8; 32] = hex!("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");

§Runtime hex parsing

// In your manifest use the `package` key to improve import ergonomics.
// hex = { package = "hex-conservative", version = "*" }
use hex::prelude::*;

// Decode an arbitrary length hex string into a vector.
let v = hex::decode_to_vec("deadbeef").expect("valid hex digits");
// Or a known length hex string into a fixed size array.
let a = hex::decode_to_array::<4>("deadbeef").expect("valid length and valid hex digits");

// We support `LowerHex` and `UpperHex` out of the box for `[u8]` slices.
println!("An array as lower hex: {:x}", a.as_hex());
// And for vecs since `Vec` derefs to byte slice.
println!("A vector as upper hex: {:X}", v.as_hex());

// Allocate a new string (also `to_upper_hex_string`).
let s = v.to_lower_hex_string();

// Please note, mixed case strings will still parse successfully but we only
// support displaying hex in a single case.
assert_eq!(
    hex::decode_to_vec("dEaDbEeF").expect("valid mixed case hex digits"),
    hex::decode_to_vec("deadbeef").expect("valid hex digits"),
);

§Crate feature flags

  • std - enables the standard library, on by default.
  • alloc - enables features that require allocation such as decoding into Vec<u8>, implied by std.
  • newer-rust-version - enables Rust version detection and thus newer features, may add dependency on a feature detection crate to reduce compile times. This feature is expected to do nothing once the native detection is in Rust and our MSRV is at least that version. We may also remove the feature gate in 2.0 with semver trick once that happens.

§Minimum Supported Rust Version (MSRV)

The current MSRV is Rust 1.74.0. Policy is to never use an MSRV that is less than two years old and also that ships in Debian stable. We may bump our MSRV in a minor version, but we have no plans to.

Note though that the dependencies may have looser policy. This is not considered breaking/wrong

  • you would just need to pin them in Cargo.lock (not .toml).

Re-exports§

pub use self::error::DecodeFixedLengthBytesError;
pub use self::error::DecodeVariableLengthBytesError;
pub use self::error::InvalidCharError;
pub use self::error::InvalidLengthError;
pub use self::error::OddLengthStringError;

Modules§

buf_encoder
Implements a buffered encoder.
display
Helpers for displaying bytes as hex strings.
error
The error types.
prelude
Re-exports of the common crate traits.

Macros§

fmt_hex_exact
Format known-length array as hex.
hex
Parses hex strings in const contexts.
impl_fmt_traits
Adds core::fmt trait implementations to type $ty.

Structs§

BytesToHexIter
Iterator over bytes which encodes the bytes and yields [Char; 2] pairs of hex characters.
HexSliceToBytesIter
Iterator over bytes decoded from a hex string slice.
HexToBytesIter
Iterator yielding bytes decoded from an iterator of pairs of hex digits.

Enums§

Case
Possible case of hex.
Char
A valid hex character: one of [0-9a-fA-F].

Traits§

DisplayHex
Extension trait for types that can be displayed as hex.

Functions§

decode_to_array
Decodes a hex string with an expected length known at compile time.
decode_to_vecalloc
Decodes a hex string with variable length.