#file-io #read #binary #file #write #read-write #file-read

file-utils

Convenience wrappers for file I/O, mostly around binary operations

6 releases

Uses old Rust 2015

0.1.5 Jun 9, 2018
0.1.4 Mar 18, 2018

#1610 in Encoding

Download history 6/week @ 2023-12-14 12/week @ 2023-12-21 6/week @ 2024-01-18 27/week @ 2024-01-25 10/week @ 2024-02-15 19/week @ 2024-02-22 11/week @ 2024-02-29 11/week @ 2024-03-07 17/week @ 2024-03-14 10/week @ 2024-03-21 13/week @ 2024-03-28

51 downloads per month

Custom license

15KB
445 lines

file-utils

This crate aims to provide convenient one-liners for file I/O operations that carry no dependencies and don't require unsafe code.

Furthermore, to ensure that multi-byte primitive types and pointers like usize are encoded correctly, compilation of the crate takes into account:

  • Architecture (32-bit vs 64-bit)
  • Endianness

Usage

Reading binary

All the methods are implemented directly for any type that implements the Read trait, so all you need to do is bring the traits into scope.

extern crate file_utils;

use std::io;
use std::fs::File;

use file_utils::read::Read;		// <--- bring the Read trait into scope

fn foo() -> io::Result<()>
{
	let mut file = File::open("binary-file")?;

	// Read the first 8 bytes of the file into a u64
	let uns: u64 = file.read_u64()?;

	// And the next 4 bytes into an f32
	let flt: f32 = file.read_f32()?;

	Ok(())
}

All primitive number types can be read this way

fn read_usize(&mut self)-> io::Result<usize>;
fn read_isize(&mut self)-> io::Result<isize>;

// 8-bit
fn read_u8(&mut self)-> io::Result<u8>;
fn read_i8(&mut self)-> io::Result<i8>;

// 16-bit
fn read_u16(&mut self)-> io::Result<u16>;
fn read_i16(&mut self)-> io::Result<i16>;

// 32-bit
fn read_u32(&mut self)-> io::Result<u32>;
fn read_i32(&mut self)-> io::Result<i32>;
fn read_f32(&mut self)-> io::Result<f32>;

// 64-bit
fn read_u64(&mut self)-> io::Result<u64>;
fn read_i64(&mut self)-> io::Result<i64>;
fn read_f64(&mut self)-> io::Result<f64>;

Writing binary

Similarly to Read, this crate provides its writing methods as implementations to any type that implements Write.

extern crate file_utils;

use std::io;
use std::fs::File;

use file_utils::write::Write;		// <--- bring the Write trait into scope

fn foo() -> io::Result<()>
{
	let file mut = File::create("binary-file")?;

	// Write a usize to the file, which will either be 4 or 8 bytes depending on architecture
	file.write_usize(12)?;

	// Write a 4-byte floating point number
	file.write_f32(1.42)?;

	Ok(())
}

No runtime deps