Skip to content

Commit 3c7a7f2

Browse files
committed
disks: Deref disks for simplicity
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 078d6ac commit 3c7a7f2

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

crates/disks/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,32 @@ impl Disk {
7777
/// Returns the name of the disk device.
7878
pub fn name(&self) -> &str {
7979
match self {
80-
Disk::Scsi(disk) => &disk.disk.name,
81-
Disk::Nvme(disk) => &disk.disk.name,
80+
Disk::Scsi(disk) => &disk.name,
81+
Disk::Nvme(disk) => &disk.name,
8282
}
8383
}
8484

8585
/// Returns the partitions on the disk.
8686
pub fn partitions(&self) -> &[Partition] {
8787
match self {
88-
Disk::Scsi(disk) => &disk.disk.partitions,
89-
Disk::Nvme(disk) => &disk.disk.partitions,
88+
Disk::Scsi(disk) => &disk.partitions,
89+
Disk::Nvme(disk) => &disk.partitions,
9090
}
9191
}
9292

9393
/// Returns the path to the disk device in dev.
9494
pub fn device_path(&self) -> &Path {
9595
match self {
96-
Disk::Scsi(disk) => &disk.disk.device,
97-
Disk::Nvme(disk) => &disk.disk.device,
96+
Disk::Scsi(disk) => &disk.device,
97+
Disk::Nvme(disk) => &disk.device,
9898
}
9999
}
100100

101101
/// Returns the total number of sectors on the disk.
102102
pub fn sectors(&self) -> u64 {
103103
match self {
104-
Disk::Scsi(disk) => disk.disk.sectors,
105-
Disk::Nvme(disk) => disk.disk.sectors,
104+
Disk::Scsi(disk) => disk.sectors,
105+
Disk::Nvme(disk) => disk.sectors,
106106
}
107107
}
108108

@@ -114,16 +114,16 @@ impl Disk {
114114
/// Returns the model name of the disk.
115115
pub fn model(&self) -> Option<&str> {
116116
match self {
117-
Disk::Scsi(disk) => disk.disk.model.as_deref(),
118-
Disk::Nvme(disk) => disk.disk.model.as_deref(),
117+
Disk::Scsi(disk) => disk.model.as_deref(),
118+
Disk::Nvme(disk) => disk.model.as_deref(),
119119
}
120120
}
121121

122122
/// Returns the vendor name of the disk.
123123
pub fn vendor(&self) -> Option<&str> {
124124
match self {
125-
Disk::Scsi(disk) => disk.disk.vendor.as_deref(),
126-
Disk::Nvme(disk) => disk.disk.vendor.as_deref(),
125+
Disk::Scsi(disk) => disk.vendor.as_deref(),
126+
Disk::Nvme(disk) => disk.vendor.as_deref(),
127127
}
128128
}
129129
}

crates/disks/src/nvme.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@
99
1010
use crate::{BasicDisk, DiskInit};
1111
use regex::Regex;
12-
use std::{path::Path, sync::OnceLock};
12+
use std::{ops::Deref, path::Path, sync::OnceLock};
1313

1414
/// Regex pattern to match valid NVMe device names (e.g. nvme0n1)
1515
static NVME_PATTERN: OnceLock<Regex> = OnceLock::new();
1616

1717
/// Represents an NVMe disk device
1818
#[derive(Debug)]
19-
pub struct Disk {
20-
/// The underlying basic disk implementation
21-
pub(crate) disk: BasicDisk,
19+
pub struct Disk(pub BasicDisk);
20+
21+
impl Deref for Disk {
22+
type Target = BasicDisk;
23+
24+
fn deref(&self) -> &Self::Target {
25+
&self.0
26+
}
2227
}
2328

2429
impl DiskInit for Disk {
@@ -35,9 +40,7 @@ impl DiskInit for Disk {
3540
let regex = NVME_PATTERN
3641
.get_or_init(|| Regex::new(r"^nvme\d+n\d+$").expect("Failed to initialise known-working regex"));
3742
if regex.is_match(name) {
38-
Some(Self {
39-
disk: BasicDisk::from_sysfs_path(sysroot, name)?,
40-
})
43+
Some(Self(BasicDisk::from_sysfs_path(sysroot, name)?))
4144
} else {
4245
None
4346
}

crates/disks/src/scsi.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@
88
//! the SCSI subsystem. This module handles enumeration and management of these devices,
99
//! which appear as `/dev/sd*` block devices.
1010
11-
use std::path::Path;
11+
use std::{ops::Deref, path::Path};
1212

1313
use crate::{BasicDisk, DiskInit};
1414

1515
/// Represents a SCSI disk device.
1616
///
1717
/// This struct wraps a BasicDisk to provide SCSI-specific functionality.
1818
#[derive(Debug)]
19-
pub struct Disk {
20-
pub(crate) disk: BasicDisk,
19+
pub struct Disk(pub BasicDisk);
20+
21+
impl Deref for Disk {
22+
type Target = BasicDisk;
23+
24+
fn deref(&self) -> &Self::Target {
25+
&self.0
26+
}
2127
}
2228

2329
impl DiskInit for Disk {
@@ -35,9 +41,7 @@ impl DiskInit for Disk {
3541
fn from_sysfs_path(sysroot: &Path, name: &str) -> Option<Self> {
3642
let matching = name.starts_with("sd") && name[2..].chars().all(char::is_alphabetic);
3743
if matching {
38-
Some(Self {
39-
disk: BasicDisk::from_sysfs_path(sysroot, name)?,
40-
})
44+
Some(Self(BasicDisk::from_sysfs_path(sysroot, name)?))
4145
} else {
4246
None
4347
}

0 commit comments

Comments
 (0)