Skip to main content

FileBuffer

Struct FileBuffer 

Source
pub struct FileBuffer { /* private fields */ }
Expand description

A memory-mapped file buffer for efficient file access

This struct provides safe access to file contents through memory mapping, which avoids loading the entire file into memory while providing fast random access to file data.

§Examples

use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
let data = buffer.as_slice();
println!("File size: {} bytes", data.len());

Implementations§

Source§

impl FileBuffer

Source

pub fn new(path: &Path) -> Result<Self, IoError>

Creates a new memory-mapped file buffer

§Arguments
  • path - Path to the file to be mapped
§Returns

Returns a FileBuffer on success, or an IoError if the file cannot be opened or mapped.

§Errors

This function will return an error if:

  • The file does not exist or cannot be opened
  • The file cannot be memory-mapped
  • The file is empty
  • The file is larger than the maximum allowed size
  • File metadata cannot be read
§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;

Creates a symlink in a cross-platform manner

§Arguments
  • original - The path to the original file or directory
  • link - The path where the symlink should be created
§Errors
  • Returns std::io::Error if symlink creation fails (e.g., insufficient permissions)
  • On Windows, may require admin privileges or developer mode enabled
  • On non-Unix/Windows platforms, returns an “Unsupported” error
Source

pub fn as_slice(&self) -> &[u8]

Returns the file contents as a byte slice

This provides safe access to the memory-mapped file data without copying the contents.

§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
let data = buffer.as_slice();
println!("First byte: 0x{:02x}", data[0]);
Source

pub fn path(&self) -> &Path

Returns the path of the file

§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
println!("File path: {}", buffer.path().display());
Source

pub fn len(&self) -> usize

Returns the size of the file in bytes

§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
println!("File size: {} bytes", buffer.len());
Source

pub fn is_empty(&self) -> bool

Returns true if the file is empty

Note: This should never return true for a successfully created FileBuffer, as empty files are rejected during construction.

§Examples
use libmagic_rs::io::FileBuffer;
use std::path::Path;

let buffer = FileBuffer::new(Path::new("example.bin"))?;
assert!(!buffer.is_empty()); // Should always be false for valid buffers

Trait Implementations§

Source§

impl Debug for FileBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for FileBuffer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.