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
impl FileBuffer
Sourcepub fn new(path: &Path) -> Result<Self, IoError>
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"))?;Sourcepub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(
original: P,
link: Q,
) -> Result<(), Error>
pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>( original: P, link: Q, ) -> Result<(), Error>
Creates a symlink in a cross-platform manner
§Arguments
original- The path to the original file or directorylink- The path where the symlink should be created
§Errors
- Returns
std::io::Errorif 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
Sourcepub fn as_slice(&self) -> &[u8] ⓘ
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]);Sourcepub fn path(&self) -> &Path
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());Sourcepub fn len(&self) -> usize
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());Sourcepub fn is_empty(&self) -> bool
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