pub struct IoBlockDevice<T> { /* private fields */ }Expand description
A block device backed by any type implementing Read + Write + Seek.
This is useful for using files, memory buffers, or any seekable I/O as a block device.
Implementations§
Source§impl<T> IoBlockDevice<T>
impl<T> IoBlockDevice<T>
Sourcepub fn new(inner: T, block_size: u32, total_size: u64) -> Self
pub fn new(inner: T, block_size: u32, total_size: u64) -> Self
Create a new IoBlockDevice wrapping the given I/O object.
§Arguments
inner- The underlying I/O objectblock_size- Block size in bytes (typically 512 or 4096)total_size- Total size of the device in bytes
Examples found in repository?
examples/mkfs_file.rs (line 27)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let path = "test_disk.img";
11 let size: u64 = 10 * 1024 * 1024; // 10 MB
12
13 println!("Creating disk image: {} ({} bytes)", path, size);
14
15 // Create and size the file
16 {
17 let mut file = File::create(path)?;
18 file.seek(SeekFrom::Start(size - 1))?;
19 file.write_all(&[0])?;
20 file.sync_all()?;
21 }
22
23 // Open for read/write
24 let file = OpenOptions::new().read(true).write(true).open(path)?;
25
26 // Create block device wrapper (512-byte sectors)
27 let device = IoBlockDevice::new(file, 512, size);
28
29 // Configure and create filesystem
30 let config = MkfsConfig::new()
31 .fs_type(FsType::Ext4)
32 .block_size(4096)
33 .label("test_volume");
34
35 println!("Formatting as ext4...");
36 mkfs(device, config)?;
37
38 println!("Done! You can verify with: file {} or dumpe2fs {}", path, path);
39
40 Ok(())
41}Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the wrapper and return the underlying I/O object.
Trait Implementations§
Source§impl<T> BlockDevice for IoBlockDevice<T>
impl<T> BlockDevice for IoBlockDevice<T>
Source§fn block_size(&self) -> u32
fn block_size(&self) -> u32
Returns the physical block size in bytes.
Source§fn block_count(&self) -> u64
fn block_count(&self) -> u64
Returns the total number of blocks.
Source§fn read_blocks(
&mut self,
buf: &mut [u8],
block_id: u64,
block_count: u32,
) -> Result<()>
fn read_blocks( &mut self, buf: &mut [u8], block_id: u64, block_count: u32, ) -> Result<()>
Read blocks from the device. Read more
Auto Trait Implementations§
impl<T> Freeze for IoBlockDevice<T>where
T: Freeze,
impl<T> RefUnwindSafe for IoBlockDevice<T>where
T: RefUnwindSafe,
impl<T> Send for IoBlockDevice<T>where
T: Send,
impl<T> Sync for IoBlockDevice<T>where
T: Sync,
impl<T> Unpin for IoBlockDevice<T>where
T: Unpin,
impl<T> UnwindSafe for IoBlockDevice<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more