2.1 `zerocopy`

01-zerocopy — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/bare-metal/useful-crates/zerocopy.html

2.1 zerocopy

这 zerocopy crate(来自 Fuchsia)提供安全的特征和宏 在字节序列和其他类型之间进行转换。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
use zerocopy::{Immutable, IntoBytes};

#[repr(u32)]
#[derive(Debug, Default, Immutable, IntoBytes)]
enum RequestType {
    #[default]
    In = 0,
    Out = 1,
    Flush = 4,
}

#[repr(C)]
#[derive(Debug, Default, Immutable, IntoBytes)]
struct VirtioBlockRequest {
    request_type: RequestType,
    reserved: u32,
    sector: u64,
}

fn main() {
    let request = VirtioBlockRequest {
        request_type: RequestType::Flush,
        sector: 42,
        ..Default::default()
    };

    assert_eq!(
        request.as_bytes(),
        &[4, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0]
    );
}

这不适合 MMIO(因为它不使用易失性读取和写入),但是 对于处理与硬件共享的结构非常有用,例如通过 DMA,或 通过一些外部接口发送。

  • FromBytes可以针对任何字节模式都有效的类型来实现, 因此可以安全地从不受信任的字节序列进行转换。
  • 尝试推导FromBytes对于这些类型会失败,因为 RequestType不使用所有可能的 u32 值作为判别式,因此并非所有 字节模式有效。
  • zerocopy::byteorder具有字节顺序感知数字原语的类型。
  • 运行该示例cargo run在下面 src/bare-metal/useful-crates/zerocopy-example/。 (它不会运行在 由于板条箱依赖性,游乐场。)
最后修改 August 11, 2026: 更新 (70a5af133)