#uuid #data #conversion #fourcc

no-std four-cc

Newtype wrapper providing a convenient representation of four-character-code values

3 releases (breaking)

0.3.0 May 30, 2023
0.2.0 May 22, 2022
0.1.0 Jul 15, 2020

#299 in Algorithms

Download history 3844/week @ 2023-06-11 3987/week @ 2023-06-18 4952/week @ 2023-06-25 1981/week @ 2023-07-02 1726/week @ 2023-07-09 1481/week @ 2023-07-16 1587/week @ 2023-07-23 2448/week @ 2023-07-30 2806/week @ 2023-08-06 1639/week @ 2023-08-13 2937/week @ 2023-08-20 4636/week @ 2023-08-27 3202/week @ 2023-09-03 2395/week @ 2023-09-10 2253/week @ 2023-09-17 3879/week @ 2023-09-24

11,928 downloads per month
Used in 20 crates (6 directly)

MIT/Apache

10KB
164 lines

Newtype wrapper providing a convenient representation of four-character-code values.

Using this type in a public APIs as an alternative to simply passing the equivalent u32 makes the value's expected use explicit.

Creating a FourCC value

use four_cc::FourCC;

let uuid = FourCC(*b"uuid");

From a slice

let data = b"moofftyp";
let code = FourCC::from(&data[0..4]);  // would panic if fewer than 4 bytes
assert_eq!(FourCC(*b"moof"), code);

Constants

FourCC values can be used in const expressions

const UUID: FourCC = FourCC(*b"uuid");

Matching

You can use FourCC values in match patterns as long as you define constants to match against,

const UUID: FourCC = FourCC(*b"uuid");
const MOOV: FourCC = FourCC(*b"moov");
match other_value {
    MOOV => println!("movie"),
    UUID => println!("unique identifier"),
    // compiler will not accept: FourCC(*b"trun") => println!("track fragment run"),
    _ => println!("Other value; scary stuff")
}

Invalid literal values

If the literal has other than four bytes, compilation will fail

let bad_fourcc = FourCC(*b"uuid123");
// -> expected an array with a fixed size of 4 elements, found one with 7 elements

Note the FourCC value may contain non-printable byte values, including the byte-value zero.

Debug display

let uuid = FourCC(*b"uuid");
println!("it's {:?}", uuid);  // produces: it's FourCC{uuid}

Note that if the FourCC bytes are not able to be converted to UTF8, then a fallback representation will be used (as it would be suprising for format!() to panic).

let uuid = FourCC(*b"u\xFFi\0");
println!("it's {:?}", uuid);  // produces: it's FourCC{u\xffi\x00}

Dependencies

~0–365KB