Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Generic function using variable of unknown size

Name: Anonymous 2019-05-21 7:35

For no particular reason I'm endeavoring to make sense of how to compose a conventional capacity that takes a std::fs::File and peruses measure <T> from the record into <T>. It feels somewhat odd to me that when attempting to make a [u8] to fill that the Rust compiler whines that it doesn't know statically the size. I can't help suspecting that it should know at arrangement dependent on <T> however. I know this is effectively conceivable. I'm simply not unreasonably acquainted with the language.

So I'm requesting help in seeing how I help the compiler get it. The code I have so far is

bar fn write_to_type<T: Sized>(f: &mut std::fs::File) - > T {
let mut bytes = [0u8; std::mem::size_of::<T>];
f.read_exact(&mut bytes).unwrap();
unsafe { std::mem::transmute::<[u8; 2], T>(bytes) }
}


The errore I'm getting is

unsized local people are gated as a temperamental component [E0277]

every single neighborhood variable must have a statically known size [E0277]

to find out additional, visit <https://doc.rust-lang.org/book/second-release/ch19-04-progressed-types.html#dynamically-sized-types-and-the-sized-trait> [E0277]

the attribute 'std::marker::Sized' isn't actualized for '[u8]' [E0277]

the size for estimations of sort '[u8]' can't be known at arrangement time (doesn't have a size known at gather time) [E0277]


Contemplations?

Name: Anonymous 2019-05-21 7:45

Rust compiler is a practical joke designed to make creation of non-trivial software impossible

Name: Anonymous 2019-05-21 11:41

Maybe you should try turning off the autothesauriser first.

Name: Anonymous 2019-05-21 15:36

>>1
Have you tried to dilate the types to bigger size?

Name: Anonymous 2019-05-22 1:28

- https://doc.rust-lang.org/std/io/trait.Read.html#method.read_to_end

use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
let mut f = File::open("foo.txt")?;
let mut buffer = Vec::new();

// read the whole file
f.read_to_end(&mut buffer)?;
Ok(())
}

Name: Anonymous 2019-05-22 3:59

>>5
Subtle.

Name: Anonymous 2019-05-22 9:27

>>6
It will turn written data into 8-bit words ;)
idk how rust does type casting

Don't change these.
Name: Email:
Entire Thread Thread List