DeviceManager

Struct DeviceManager 

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

Manager for multiple test block devices.

DeviceManager provides a higher-level API for managing multiple test block devices. It handles device creation in background threads and automatic cleanup on drop. This is the recommended way to create devices when you need to manage their lifecycle.

§Examples

use test_bd::{DeviceManager, TestBlockDeviceConfig};

let mut manager = DeviceManager::new();

// Create first device
let config1 = TestBlockDeviceConfig {
    dev_id: -1,
    size: 10 * 1024 * 1024,
    seed: 42,
    fill_percent: 50,
    duplicate_percent: 25,
    random_percent: 25,
    segments: 20,
    unprivileged: false,
};
let device1 = manager.create(config1).expect("Failed to create device 1");

// Create second device
let config2 = TestBlockDeviceConfig {
    dev_id: -1,
    size: 20 * 1024 * 1024,
    seed: 123,
    fill_percent: 33,
    duplicate_percent: 33,
    random_percent: 34,
    segments: 50,
    unprivileged: false,
};
let device2 = manager.create(config2).expect("Failed to create device 2");

// List all managed devices
for device in manager.list() {
    println!("Device {}: {} bytes", device.dev_id, device.config.size);
}

// Delete a specific device
manager.delete(device1.dev_id).expect("Failed to delete device");

// All remaining devices are automatically deleted when manager is dropped

Implementations§

Source§

impl DeviceManager

Source

pub fn new() -> Self

Creates a new DeviceManager.

§Examples
use test_bd::DeviceManager;

let manager = DeviceManager::new();
Source

pub fn create( &mut self, config: TestBlockDeviceConfig, ) -> Result<ManagedDevice, String>

Creates a new test block device and manages it.

The device is created in a background thread and this method blocks until the device is ready for I/O. The device will continue running in the background until explicitly deleted or the manager is dropped.

§Arguments
  • config - Configuration for the device
§Returns
  • Ok(ManagedDevice) - Handle to the created device
  • Err(String) - Error message if creation fails
§Examples
use test_bd::{DeviceManager, TestBlockDeviceConfig};

let mut manager = DeviceManager::new();
let config = TestBlockDeviceConfig {
    dev_id: -1,
    size: 10 * 1024 * 1024,
    seed: 42,
    fill_percent: 50,
    duplicate_percent: 25,
    random_percent: 25,
    segments: 20,
    unprivileged: false,
};

let device = manager.create(config).expect("Failed to create device");
println!("Created /dev/ublkb{}", device.dev_id);
Source

pub fn list(&self) -> Vec<ManagedDevice>

Returns a list of all currently managed devices.

§Examples
use test_bd::{DeviceManager, TestBlockDeviceConfig};

let mut manager = DeviceManager::new();
let config = TestBlockDeviceConfig {
    dev_id: -1,
    size: 10 * 1024 * 1024,
    seed: 42,
    fill_percent: 50,
    duplicate_percent: 25,
    random_percent: 25,
    segments: 20,
    unprivileged: false,
};

manager.create(config).expect("Failed to create device");

for device in manager.list() {
    println!("Device {}: {} segments", device.dev_id, device.segments.len());
}
Source

pub fn delete(&mut self, dev_id: i32) -> Result<i32, String>

Deletes a specific managed device.

This stops and removes the device, then waits for the background thread to complete. The device is removed from the manager’s tracking.

§Arguments
  • dev_id - The device ID to delete
§Returns
  • Ok(i32) - The device ID that was deleted
  • Err(String) - Error message if deletion fails
§Examples
use test_bd::{DeviceManager, TestBlockDeviceConfig};

let mut manager = DeviceManager::new();
let config = TestBlockDeviceConfig {
    dev_id: -1,
    size: 10 * 1024 * 1024,
    seed: 42,
    fill_percent: 50,
    duplicate_percent: 25,
    random_percent: 25,
    segments: 20,
    unprivileged: false,
};

let device = manager.create(config).expect("Failed to create device");
manager.delete(device.dev_id).expect("Failed to delete device");
Source

pub fn delete_all(&mut self) -> Result<(), String>

Deletes all managed devices.

This stops and removes all devices being managed. Devices are deleted sequentially. If any deletion fails, the method continues attempting to delete remaining devices and returns an error at the end.

§Returns
  • Ok(()) if all devices were deleted successfully
  • Err(String) if any deletions failed (with details about the first failure)
§Examples
use test_bd::{DeviceManager, TestBlockDeviceConfig};

let mut manager = DeviceManager::new();

// Create multiple devices
for i in 0..3 {
    let config = TestBlockDeviceConfig {
        dev_id: -1,
        size: 10 * 1024 * 1024,
        seed: i,
        fill_percent: 50,
        duplicate_percent: 25,
        random_percent: 25,
        segments: 20,
        unprivileged: false,
    };
    manager.create(config).expect("Failed to create device");
}

// Delete all devices
manager.delete_all().expect("Failed to delete all devices");

Trait Implementations§

Source§

impl Default for DeviceManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for DeviceManager

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V