rust/zerocopy/src/split_at.rs
Source file repositories/reference/linux-study-clean/rust/zerocopy/src/split_at.rs
File Facts
- System
- Linux kernel
- Corpus path
rust/zerocopy/src/split_at.rs- Extension
.rs- Size
- 37990 bytes
- Lines
- 1091
- Domain
- Rust Kernel Layer
- Bucket
- Rust API Membrane
- Inferred role
- Rust Kernel Layer: implementation source
- Status
- source implementation candidate
Why This File Exists
Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.
- Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
struct SliceDststruct Packetstruct Packetfunction via_runtime_checkfunction via_runtime_checkfunction Okfunction test_split_atfunction test_split_at_overlappingfunction test_split_at_uncheckedfunction test_split_at_via_methodsfunction test_split_at_via_unaligned
Annotated Snippet
struct SliceDst {
prefix: u8,
trailing: [u8],
}
const N: usize = 16;
let arr = [1u16; N];
let dst = SliceDst::ref_from_bytes(arr.as_bytes()).unwrap();
for i in 0..N {
let split = dst.split_at(i).unwrap().via_runtime_check();
if i % 2 == 1 {
assert!(split.is_ok());
} else {
assert!(split.is_err());
}
}
}
#[test]
fn test_split_at_unchecked() {
use crate::SplitAt;
let mut arr = [1, 2, 3, 4];
let slice = &arr[..];
// SAFETY: 2 <= arr.len() (4)
let split = unsafe { SplitAt::split_at_unchecked(slice, 2) };
// SAFETY: SplitAt::split_at_unchecked guarantees that the split is valid.
let (l, r) = unsafe { split.via_unchecked() };
assert_eq!(l, &[1, 2]);
assert_eq!(r, &[3, 4]);
let slice_mut = &mut arr[..];
// SAFETY: 2 <= arr.len() (4)
let split = unsafe { SplitAt::split_at_mut_unchecked(slice_mut, 2) };
// SAFETY: SplitAt::split_at_mut_unchecked guarantees that the split is valid.
let (l, r) = unsafe { split.via_unchecked() };
assert_eq!(l, &mut [1, 2]);
assert_eq!(r, &mut [3, 4]);
}
#[test]
fn test_split_at_via_methods() {
use crate::{FromBytes, Immutable, IntoBytes, KnownLayout, SplitAt};
#[derive(FromBytes, KnownLayout, SplitAt, IntoBytes, Immutable, Debug)]
#[repr(C)]
struct Packet {
length: u8,
body: [u8],
}
let arr = [1, 2, 3, 4];
let packet = Packet::ref_from_bytes(&arr[..]).unwrap();
let split1 = packet.split_at(2).unwrap();
let (l, r) = split1.via_immutable();
assert_eq!(l.length, 1);
assert_eq!(r, &[4]);
let split2 = packet.split_at(2).unwrap();
let (l, r) = split2.via_into_bytes();
assert_eq!(l.length, 1);
assert_eq!(r, &[4]);
}
#[test]
fn test_split_at_via_unaligned() {
use crate::{FromBytes, Immutable, IntoBytes, KnownLayout, SplitAt, Unaligned};
#[derive(FromBytes, KnownLayout, SplitAt, IntoBytes, Immutable, Unaligned)]
#[repr(C)]
struct Packet {
length: u8,
body: [u8],
}
let arr = [1, 2, 3, 4];
let packet = Packet::ref_from_bytes(&arr[..]).unwrap();
let split = packet.split_at(2).unwrap();
let (l, r) = split.via_unaligned();
assert_eq!(l.length, 1);
assert_eq!(r, &[4]);
}
}
Annotation
- Detected declarations: `struct SliceDst`, `struct Packet`, `struct Packet`, `function via_runtime_check`, `function via_runtime_check`, `function Ok`, `function test_split_at`, `function test_split_at_overlapping`, `function test_split_at_unchecked`, `function test_split_at_via_methods`.
- Atlas domain: Rust Kernel Layer / Rust API Membrane.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.