#representation #values #explicit #api #display #four-character-codes #four-character-code

no-std four-cc

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

4 releases (breaking)

0.4.0 Mar 16, 2024
0.3.0 May 30, 2023
0.2.0 May 22, 2022
0.1.0 Jul 15, 2020

#3 in #explicit

Download history 1541/week @ 2024-06-14 1378/week @ 2024-06-21 1283/week @ 2024-06-28 1260/week @ 2024-07-05 1154/week @ 2024-07-12 1498/week @ 2024-07-19 1245/week @ 2024-07-26 1346/week @ 2024-08-02 1334/week @ 2024-08-09 1249/week @ 2024-08-16 1160/week @ 2024-08-23 1236/week @ 2024-08-30 966/week @ 2024-09-06 1192/week @ 2024-09-13 1920/week @ 2024-09-20 1743/week @ 2024-09-27

5,933 downloads per month
Used in 35 crates (7 directly)

MIT/Apache

11KB
190 lines

four-cc

Representation of four-character-codes.

crates.io version Documentation Coverage Status


lib.rs:

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");

// using Into
let code: FourCC = b"uuid".into();
assert_eq!(uuid, code);

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);

From a u32

let data: u32 = 0x6d6f6f66;
let code = FourCC::from(data);
assert_eq!(FourCC(*b"moof"), code);
// conversion back into a u32
let converted: u32 = code.into();
assert_eq!(data, converted);

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 surprising for format!() to panic).

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

Dependencies

~0–495KB