Skip to main content

Crate byte_wrapper

Crate byte_wrapper 

Source
Expand description

Newtype wrappers for byte arrays and vectors with hex and base64 formatting.

This crate provides wrapper types that display byte data in human-readable encodings. HexArray<N> encodes fixed-length byte arrays as hex strings, and Base64Vec encodes variable-length byte vectors as base64 strings.

With the serde feature, this crate’s types implement Serialize and Deserialize, encoding as human-readable strings (hex or base64) in text formats like JSON, and as efficient raw bytes in binary formats like CBOR. You do not have to use the newtypes in your own type definitions; you can refer to them via #[serde(with = "...")] instead.

With the schemars08 feature, this crate’s types implement JsonSchema, including automatic opt-in replacement with typify and progenitor.

§Types

  • HexArray<N> encodes a fixed-length byte array as a hex string. (Requires the hex feature.)
  • HexSlice encodes a borrowed byte slice as a hex string. (Requires the hex feature.)
  • HexVec encodes a variable-length byte vector as a hex string. (Requires the hex and alloc features.)
  • Base64Vec encodes a variable-length byte vector as a base64 string. (Requires the base64 and alloc features.)
  • Base64Slice encodes a borrowed byte slice as a base64 string. (Requires the base64 feature.)

§Examples

use byte_wrapper::HexArray;

let h = HexArray::new([0x01, 0x02, 0xab, 0xff]);
assert_eq!(h.to_string(), "0102abff");

let parsed: HexArray<4> = "0102abff".parse().unwrap();
assert_eq!(parsed, h);

With the serde feature:

use byte_wrapper::HexArray;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Record {
    checksum: HexArray<32>,
}

Using #[serde(with = "...")] on an existing byte array:

use byte_wrapper::HexArray;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Record {
    #[serde(with = "HexArray::<32>")]
    checksum: [u8; 32],
}

§Alternatives

Several crates solve parts of this problem, often using slightly different approaches from byte-wrapper.

featurebyte-wrapperserde-human-bytes 0.1.2serde-encoded-bytes 0.2.1serde_with 3.17.0hex-buffer-serde 0.4.0hexutil 0.1.0serde-bytes-repr 0.3.0
newtype wrappersyesyes (hex only)nonononono
is_human_readable() switchyesyesyesnoyesyesno
Display / FromStr / DerefyesDeref onlynononoDisplay / FromStr via macrono
hex encodingyesyesyesyesyesyesyes
base64 encodingyesyesyesyesnonoyes
[u8; N] supportyesyesyesyesyesvia macrosno
no_stdyesyesyesyesyesyesno
JsonSchema (schemars)yesnonoyesnonono
#[serde(with)] supportyesyesyesyesyesnono

The closest alternatives are:

§Features

  • hex: enables HexArray and HexSlice, and HexVec together with alloc. Enabled by default.
  • base64: enables Base64Slice, and Base64Vec together with alloc. Enabled by default.
  • alloc: enables alloc support (required by Base64Vec and HexVec). Enabled by default.
  • serde: implements Serialize and Deserialize for enabled types. Not enabled by default.
  • schemars08: derives JsonSchema for enabled types. Requires std. Not enabled by default.

All features other than schemars08 are compatible with no_std.

Structs§

Base64Slicebase64
A byte slice that displays as base64.
Base64Vecalloc and base64
A byte vector that displays and parses as base64.
HexArrayhex
A byte array that displays and parses as hex.
HexSlicehex
A byte slice that displays as hex.
HexVecalloc and hex
A byte vector that displays and parses as hex.

Enums§

ParseBase64Erroralloc and base64
Error returned by Base64Vec::from_str.
ParseHexErrorhex
Error returned by HexArray::from_str and HexVec::from_str.