I'm trying to implement function similar to this one, what would be an idiomatic way to achieve this?
fn meta(buffer: &[u8]) -> &Meta {
let ptr = buffer as *const _ as *const Meta;
unsafe { *ptr }; // trying to return &Meta here
}
Motivation and some things to consider:
-
buffer
is a huge memory-mapped file, it's contents are outside of my control, trying to work with what's available; - what I'm trying to do here is read
Meta
, and based on it's content decide what other structs to read (in a way similar to this one) and where in buffer they are located; - I would like to avoid copying memory: while I can construct new
Meta
struct and return it as owned, I would rather let it reside in the buffer and just return reference to it (there are some structs likeMeta
which are quite big and there can exist many of them at the same time: this takes up lots of memory and performance during copying).