From b6861d0c468dc4bd0cf069327df9fccdec705054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Meier?= Date: Wed, 15 Sep 2021 19:04:50 +0200 Subject: [PATCH] relax `Path` parameter to `impl AsRef` (#71) --- examples/inspect.rs | 3 +-- src/disk.rs | 5 ++--- src/header.rs | 9 ++++++--- src/lib.rs | 2 +- src/partition.rs | 4 ++-- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/examples/inspect.rs b/examples/inspect.rs index 556c5f2..26beb23 100644 --- a/examples/inspect.rs +++ b/examples/inspect.rs @@ -20,9 +20,8 @@ fn run() -> io::Result<()> { let input = std::env::args().nth(1).unwrap_or(sample); // Open disk image. - let diskpath = std::path::Path::new(&input); let cfg = gpt::GptConfig::new().writable(false); - let disk = cfg.open(diskpath)?; + let disk = cfg.open(input)?; // Print GPT layout. println!("Disk (primary) header: {:#?}", disk.primary_header()); diff --git a/src/disk.rs b/src/disk.rs index 2595d7f..9d42ba6 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -61,11 +61,10 @@ impl fmt::Display for LogicalBlockSize { /// ## Example /// /// ```rust,no_run -/// let diskpath = std::path::Path::new("/dev/sdz"); -/// let gpt_disk = gpt::disk::read_disk(diskpath).unwrap(); +/// let gpt_disk = gpt::disk::read_disk("/dev/sdz").unwrap(); /// println!("{:#?}", gpt_disk); /// ``` -pub fn read_disk(diskpath: &path::Path) -> io::Result { +pub fn read_disk(diskpath: impl AsRef) -> io::Result> { let cfg = GptConfig::new(); cfg.open(diskpath) } diff --git a/src/header.rs b/src/header.rs index 71ddc17..d6ed8ac 100644 --- a/src/header.rs +++ b/src/header.rs @@ -296,7 +296,10 @@ impl fmt::Display for Header { /// /// let h = read_header(diskpath, lb_size).unwrap(); /// ``` -pub fn read_header(path: &Path, sector_size: disk::LogicalBlockSize) -> Result
{ +pub fn read_header( + path: impl AsRef, + sector_size: disk::LogicalBlockSize +) -> Result
{ let mut file = File::open(path)?; read_primary_header(&mut file, sector_size) } @@ -450,11 +453,11 @@ pub(crate) fn partentry_checksum( // TODO: Move this to Header::new() and Header::write to write it // that will match the Partition::write() API pub fn write_header( - p: &Path, + p: impl AsRef, uuid: Option, sector_size: disk::LogicalBlockSize, ) -> Result { - debug!("opening {} for writing", p.display()); + debug!("opening {} for writing", p.as_ref().display()); let mut file = OpenOptions::new().write(true).read(true).open(p)?; let bak = find_backup_lba(&mut file, sector_size)?; let guid = match uuid { diff --git a/src/lib.rs b/src/lib.rs index 96ceecd..8e0e0ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,7 +136,7 @@ impl GptConfig { /// Open the GPT disk at the given path and inspect it according /// to configuration options. - pub fn open(self, diskpath: &path::Path) -> io::Result { + pub fn open(self, diskpath: impl AsRef) -> io::Result> { let file = Box::new(fs::OpenOptions::new() .write(self.writable) .read(true) diff --git a/src/partition.rs b/src/partition.rs index 9065db4..5214788 100644 --- a/src/partition.rs +++ b/src/partition.rs @@ -248,11 +248,11 @@ fn read_part_name(rdr: &mut Cursor<&[u8]>) -> Result { /// println!("{:#?}", partitions); /// ``` pub fn read_partitions( - path: &Path, + path: impl AsRef, header: &Header, lb_size: disk::LogicalBlockSize, ) -> Result> { - debug!("reading partitions from file: {}", path.display()); + debug!("reading partitions from file: {}", path.as_ref().display()); let mut file = File::open(path)?; file_read_partitions(&mut file, header, lb_size) }