Skip to content

Commit

Permalink
relax Path parameter to impl AsRef<Path> (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmeier authored Sep 15, 2021
1 parent f8dd37a commit b6861d0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
3 changes: 1 addition & 2 deletions examples/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 2 additions & 3 deletions src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GptDisk> {
pub fn read_disk(diskpath: impl AsRef<path::Path>) -> io::Result<GptDisk<'static>> {
let cfg = GptConfig::new();
cfg.open(diskpath)
}
9 changes: 6 additions & 3 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Header> {
pub fn read_header(
path: impl AsRef<Path>,
sector_size: disk::LogicalBlockSize
) -> Result<Header> {
let mut file = File::open(path)?;
read_primary_header(&mut file, sector_size)
}
Expand Down Expand Up @@ -450,11 +453,11 @@ pub(crate) fn partentry_checksum<D: Read + Seek>(
// 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<Path>,
uuid: Option<uuid::Uuid>,
sector_size: disk::LogicalBlockSize,
) -> Result<uuid::Uuid> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GptDisk> {
pub fn open(self, diskpath: impl AsRef<path::Path>) -> io::Result<GptDisk<'static>> {
let file = Box::new(fs::OpenOptions::new()
.write(self.writable)
.read(true)
Expand Down
4 changes: 2 additions & 2 deletions src/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ fn read_part_name(rdr: &mut Cursor<&[u8]>) -> Result<String> {
/// println!("{:#?}", partitions);
/// ```
pub fn read_partitions(
path: &Path,
path: impl AsRef<Path>,
header: &Header,
lb_size: disk::LogicalBlockSize,
) -> Result<BTreeMap<u32, Partition>> {
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)
}
Expand Down

0 comments on commit b6861d0

Please sign in to comment.