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

无std four-cc

提供对四字符码值方便表示的新类型包装器

4个版本 (破坏性更新)

0.4.0 2024年3月16日
0.3.0 2023年5月30日
0.2.0 2022年5月22日
0.1.0 2020年7月15日

#18 in 多媒体

Download history 1938/week @ 2024-03-14 1827/week @ 2024-03-21 2414/week @ 2024-03-28 2199/week @ 2024-04-04 1741/week @ 2024-04-11 1730/week @ 2024-04-18 1466/week @ 2024-04-25 1708/week @ 2024-05-02 1588/week @ 2024-05-09 1894/week @ 2024-05-16 2076/week @ 2024-05-23 1772/week @ 2024-05-30 1144/week @ 2024-06-06 1439/week @ 2024-06-13 1491/week @ 2024-06-20 1033/week @ 2024-06-27

5,324 每月下载量
35 个软件包中使用 (直接使用6个)

MIT/Apache

11KB
190

four-cc

四字符码的表示。

crates.io version Documentation Coverage Status


lib.rs:

提供对四字符码值方便表示的新类型包装器。

在公共API中使用此类型代替简单地传递等效的 u32,可以使值的预期使用更加明确。

创建FourCC值

use four_cc::FourCC;

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

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

从切片

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

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

常量

FourCC值可用于const表达式

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

匹配

只要定义了匹配的常量,就可以在match模式中使用FourCC值

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

无效的字面值

如果字面值不是四个字节,编译将失败

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

注意 FourCC值可能包含非打印字节值,包括字节值零。

调试显示

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

请注意,如果FourCC字节无法转换为UTF8,则将使用后备表示(因为它会导致format!()崩溃)。

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

依赖项

~0–455KB