8.7.2.2 使用整数偏移
02-使用整数偏移 — Comprehensive Rust
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/unsafe-deep-dive/pinning/self-referential-buffer/rust-offset.html
8.7.2.2 使用整数偏移
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
| // Copyright 2026 Google LLC
// SPDX-License-Identifier: Apache-2.0
#[derive(Debug)]
pub struct SelfReferentialBuffer {
data: [u8; 1024],
position: usize,
}
impl SelfReferentialBuffer {
pub fn new() -> Self {
SelfReferentialBuffer { data: [0; 1024], position: 0 }
}
pub fn read(&self, n_bytes: usize) -> &[u8] {
let available = self.data.len().saturating_sub(self.position);
let len = n_bytes.min(available);
&self.data[self.position..self.position + len]
}
pub fn write(&mut self, bytes: &[u8]) {
let available = self.data.len().saturating_sub(self.position);
let len = bytes.len().min(available);
self.data[self.position..self.position + len].copy_from_slice(&bytes[..len]);
self.position += len;
}
}
|
在 Rust 中,更惯用的做法是使用偏移变量,并在需要时创建引用。