Home
Search
Rust transmute copy ExampleInvoke the transmute copy function to convert bytes into a struct in an unsafe, efficient way.
Transmute copy. In Rust it is possible to directly convert a byte slice into a struct. This can be done with the transmute_copy function—type arguments are required.
By using try_into and unwrap, we can convert a range of bytes into the required argument. The transmute_copy function is unsafe, and the resulting struct may be invalid.
Example code. To begin, we have a Node struct that has two 32-bit unsigned integers in it. Each Node is 8 bytes. We have the "packed" repr to ensure the data has a consistent layout in memory.
Source In main() we have a source vector with 16 bytes—this could be read in from a file.
vec
Transmute copy We invoke transmute_copy() twice on the source data. The offset local indicates where we start reading the bytes.
Try into We take a slice of the data in the argument to transmute_copy. This is fast as it references existing data.
use std::mem; #[repr(packed)] #[derive(Debug)] struct Node { id: u32, data: u32, } fn main() { // The input data containing the 2 structs in byte form. let source = vec![0, 2, 10, 4, 200, 100, 2, 4, 0, 40, 10, 2, 1, 50, 20, 7]; // Use transmute_copy to convert the raw bytes into structs. unsafe { // First struct. let offset = 0; let node1 = mem::transmute_copy::<[u8; 8], Node>(&source[offset..offset+8].try_into().unwrap()); // Second struct (offset is 8). let offset = 8; let node2 = mem::transmute_copy::<[u8; 8], Node>(&source[offset..offset+8].try_into().unwrap()); // Print the structs. println!("{:?}", node1); println!("{:?}", node2); } }
Node { id: 67764736, data: 67265736 } Node { id: 34220032, data: 118764033 }
A warning. The transmute_copy function does not ensure the resulting struct will be valid. But if you have complete control over the data source, the program will work correctly.
However Using a function like from_ne_bytes() to read in the data would avoid the need for unsafe code. It would likely be slower.
Bytes, Integer
With transmute_copy, we can convert bytes to a struct in an extremely fast and direct way. But this conversion is unsafe—the compiler cannot prove it is correct.
Home
© 2007-2022 sam allen.
see site info on the changelog.