On 2014-12-10 19:08, Matt wrote:
Sorry if this has been covered before, and also that I'm a complete noob, but 
I'm thinking about trying a first project in Rust, and trying to learn enough 
to get started.

My plan is for an on-disk key-value store. I'm going to end up writing arrays 
of numbers to disk, and then needing to efficiently read them out of a mmap-ed 
file. So I'm wondering how in Rust you efficiently/zero-copy-ly take some slice 
of a read-only mmap and treat it as e.g. a vector of ints?

I see Vec.from_raw_parts() and Vec.from_raw_buf(), but I don't really 
understand the difference between them, and also they seem like they just give 
you a vector of the pointer's type, so I don't know how you use them convert 
the u8s you get from MemoryMap.data() into a vector of a different type, e.g. 
32 bit ints.

It seems like there should be a higher level API for this kind of thing, where 
"casting" a slice of a read-only memory buffer into an immutable vector is not 
an unsafe operation (I mean, you can do that in Python ;) Either I don't see it in the 
docs, or it doesn't exist yet; just wondering which :)

Maybe this function will help you:

http://doc.rust-lang.org/std/os/struct.MemoryMap.html

At least on Linux you should be able to open a file, get its fd using as_raw_fd() and pass that into the call to MemoryMap::new (see MapOption::MapFd()).

// David
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to