I am getting this exception when I try to run my test:
thread 'persistent_log::doc::test::test_sync' panicked at 'called `Result::unwrap()` on an `Err` value: IoError(Error { repr: Os { code: 9, message: "Bad file descriptor" } })', ../src/libcore/result.rs:783
Term:
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord,RustcEncodable,RustcDecodable)]
pub struct Term(u64);
Test:
fn create() {
File::create(&"term");
}
#[test]
fn test_sync() {
create();
let mut f = File::open(&"term").unwrap();
let term = Term(10);
encode_into(&term, &mut f, SizeLimit::Infinite).unwrap();
let decoded_term: Term = decode_from(&mut f, SizeLimit::Infinite).unwrap();
assert_eq!(decoded_term, term);
}
I want to write the object term into a file and afterwards read it in.
File::open opens a file in read-only mode, so writing to it will fail.
To open a file with read and write access, you must use OpenOptions.
use std::fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("term");
Great, but now I get a new exception:
thread 'persistent_log::doc::test::test_sync' panicked at 'called Result::unwrap() on an Err value: IoError(Error { repr: Custom(Custom { kind: UnexpectedEof, error: StringError("failed to fill whole buffer") }) })', ../src/libcore/result.rs:783
After you've written bytes to the file, the file's cursor will be located at the end of the file. Before you try read from the file, you'll have to seek back to the start of the file, otherwise you'll get this error.
file.seek(SeekFrom::Start(0));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With