last day (16 days later) » 

15:42
33
A: How can I convert a buffer of a slice of bytes (&[u8]) to an integer?

ShepmasterI'd suggest using the byteorder crate (which also works in a no-std environment): use byteorder::{BigEndian, ReadBytesExt}; // 1.2.7 fn main() { let mut buf: &[u8] = &[0, 0, 0, 1]; let num = buf.read_u32::<BigEndian>().unwrap(); assert_eq!(1, num); } This handles oddly-sized slic...

What if buf is a vec<8>?
@user2284570 you don't have to do anything special for a Vec<u8>. See also How do I get a slice of a Vec<T> in Rust?. You have to care about the endianness of the data. It's the difference between 12345 and 54321. So long as both the writer and the reader agree on the endianness of the data, you are fine. If you are writing it, then pick one and use it consistently. Most people would pick little endian, probably.
@Shepmaster it s rather that I need to preserve the existing byte ordering and that the code will run on cpu supportting unaligned memory access. So how to do the same without from_be_bytes since it seems there s no from_ptr() function? It also seems Vec has no read_u32.
@Shepmaster and for doing it on array while still using NativeEndian?
15:42
@user2284570 It's not clear what part you are missing. example
@Shepmaster sorry I was meaning without using the byteorder crate.
Why not use the crate?
Otherwise
70
Q: How to get a slice as an array in Rust?

JeroenI have an array of an unknown size, and I would like to get a slice of that array and convert it to a statically sized array: fn pop(barry: &[u8]) -> [u8; 3] { barry[0..3] // expected array `[u8; 3]`, found slice `[u8]` } How would I do this?

or, in the rawest way...
Because latency is an issue as this is a race against a competitor and that I want to avoid stack and register saving and out of all accessing unitizlized memory after allocation?
I'd expect that the optimizer will take care of all that, TBH
but, since you are that worried, then you can run your benchmarks with both implementations
and see which is faster.
16:01
@Shepmaster a last unrelated thing, how I can write at a fixed compile time memory location? For example, *0xbedfade=3..
16:20
@Shepmaster it s a call to a compiled library so it wont get inlined. it simply uses mem::transmute under the hoods. How to do solething like if 42==std::mem::transmute(buf)?
16:34
Where buf is longer than 8 elements
16:49
> a call to a compiled library so it wont get inlined
This isn't relevant.
If you were to use byteorder, or any of the suggestions i've provided, they will be optimized in whatever code you compile
so if you make a dynamic or static library, as it seems you are saying
then that library will be optimized and abstractions can be removed
you need to cast the pointer value as appropriate
but there's no real reason to use transmute here
@Shepmaster is there a playground for viewing the compiled assembly?
You need to switch to release mode to enable the optimizer
and usually need to make the function public
playground::demo:
	movl	(%rdi), %eax
	retq
 
6 hours later…
23:16
@Shepmaster howecer, because ptr::read() takes a a pointer as parameter, a constant, is first put on the stack and the value on the stack (innorder be read as a pointer) is then put inside a register for reading even with optimizations enabled. you can have something movl $*ds:581684,r15 directly as I understand. C reimains superior for that case.
Since I m having lot of shared memory at fixed location, maybe I should write a C module just for that. Or is rust able to read or write to memory usint a constant direcrly instead of using a pointer to it?

  last day (16 days later) »