[pbs-devel] [PATCH proxmox-backup v2 2/3] disks: use builder pattern for querying disk usage

Wolfgang Bumiller w.bumiller at proxmox.com
Thu Jun 9 12:38:49 CEST 2022


On Wed, Jun 08, 2022 at 08:51:52AM +0000, Hannes Laimer wrote:
(...)
> diff --git a/src/tools/disks/mod.rs b/src/tools/disks/mod.rs
> index ea4c687a..a1436fc3 100644
> --- a/src/tools/disks/mod.rs
> +++ b/src/tools/disks/mod.rs
> @@ -781,19 +781,77 @@ fn scan_partitions(
>      Ok(used)
>  }
>  
> -/// Get disk usage information for a single disk
> -pub fn get_disk_usage_info(
> -    disk: &str,
> -    no_smart: bool,
> -    include_partitions: bool,
> -) -> Result<DiskUsageInfo, Error> {
> -    let mut filter = Vec::new();
> -    filter.push(disk.to_string());
> -    let mut map = get_disks(Some(filter), no_smart, include_partitions)?;
> -    if let Some(info) = map.remove(disk) {
> -        Ok(info)
> -    } else {
> -        bail!("failed to get disk usage info - internal error"); // should not happen
> +pub struct DisksUsageQuery {

Instead of having two types named almost the same with the same
parameters apart from disks, I think it would make more sense to just
have 2 'query' methods on a single type pass the disk list to one of the
query functions.

Also, it probably makese sense to use a slice (&[String]) instead of an owned
vector anyway in get_disks.

> +    disks: Option<Vec<String>>,
> +    smart: bool,
> +    partitions: bool,
> +}
> +
> +impl DisksUsageQuery {
> +    pub fn new() -> DisksUsageQuery {
> +        DisksUsageQuery {
> +            disks: None,
> +            smart: true,
> +            partitions: false,
> +        }
> +    }
> +
> +    pub fn disks<'a>(&'a mut self, disks: Vec<String>) -> &'a mut DisksUsageQuery {

You don't need to name the lifetimes at all here (in any of the
methods), and you can use `Self` for the type (`&mut Self`).





More information about the pbs-devel mailing list