Hi everyone,
I want to serialize certain data types into a vector of bytes ~[u8].
Most notably the integers and floats up to 64 bit.
I also want to extract data from (the middle of) a ~[u8] vector back.
Since this involves unsafe pointer operations, the size of the type is
relevant for boundary checking.
trait BaseType {
fn to_binary(&self) -> ~[u8];
fn from_binary(bin: ~[u8], index: uint) -> Option<Self> {
let size = std::sys::size_of<Self>(); // the code works if size
is set to a fixed value (say 8 or 16)
if bin.len() >= index+(size/8) {
Some(extract(bin,(index*8)/size))) } else { None }
}
}
fn extract<T>(bin: ~[u8], index: uint) -> T {
unsafe {
let ptr = std::vec::raw::to_ptr(bin) as *T;
std::vec::raw::from_buf_raw(ptr, bin.len())[index]
}
}
The compiler aborts with the following message:
datatypes.rs:10:37: 10:41 error: unresolved name `Self`. Did you mean `bin`?
datatypes.rs:10 let size = std::sys::size_of<Self>()/8;
Is it currently possible to access the size (in bits/bytes) of the
"Self"-type?
My rustc has version 0.8-pre (Ubuntu ppa rust-daily).
Best regards,
Julius
PS: I looked at Bitv, yet chose to implement as a fairly simple trait
for my use case.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev