Skip to content

Commit 852c4e8

Browse files
committed
Update debug implementation to no longer require encoding to implement debug
1 parent 5202519 commit 852c4e8

14 files changed

Lines changed: 117 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
## [0.2.2] - 2023-02-14
10+
## [0.3.0] - 2023-02-14
1111

1212
* Add `Clone` implementation for `Box<Path<T>>` and `Box<Utf8Path<T>>`
13+
* Fix `Clone` implementation for `PathBuf<T>` and `Utf8PathBuf<T>` requiring a
14+
clone implementation for the encoding, which is not necessary
15+
([#5](https://github.com/chipsenkbeil/typed-path/issues/5))
16+
* Update `Debug` implementation for `Path<T>`, `Utf8Path<T>`, `PathBuf<T>`, and
17+
`Utf8PathBuf<T>` to no longer require debug implementation for encoding,
18+
which is not necessary
19+
* Add `label` method to encoding implementations, used for debugging purposes
1320

1421
## [0.2.1] - 2022-12-12
1522

@@ -28,7 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2835

2936
Initial release of the library!
3037

31-
[Unreleased]: https://github.com/chipsenkbeil/typed-path/compare/v0.2.1...HEAD
38+
[Unreleased]: https://github.com/chipsenkbeil/typed-path/compare/v0.3.0...HEAD
39+
[0.3.0]: https://github.com/chipsenkbeil/typed-path/compare/v0.2.1...v0.3.0
3240
[0.2.1]: https://github.com/chipsenkbeil/typed-path/compare/v0.2.0...v0.2.1
3341
[0.2.0]: https://github.com/chipsenkbeil/typed-path/compare/v0.1.0...v0.2.0
3442
[0.1.0]: https://github.com/chipsenkbeil/typed-path/releases/tag/v0.1.0

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "typed-path"
33
description = "Provides typed variants of Path and PathBuf for Unix and Windows"
4-
version = "0.2.2"
4+
version = "0.3.0"
55
edition = "2021"
66
authors = ["Chip Senkbeil <chip@senkbeil.org>"]
77
categories = ["development-tools", "filesystem", "os"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ and Windows.
2020

2121
```toml
2222
[dependencies]
23-
typed-path = "0.2"
23+
typed-path = "0.3"
2424
```
2525

2626
## Why?

src/common/non_utf8.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub trait Encoding<'a>: private::Sealed {
2020
/// Represents the type of component that will be derived by this encoding
2121
type Components: Components<'a>;
2222

23+
/// Static label representing encoding type
24+
fn label() -> &'static str;
25+
2326
/// Produces an iterator of [`Component`]s over the given the byte slice (`path`)
2427
fn components(path: &'a [u8]) -> Self::Components;
2528

src/common/non_utf8/path.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,11 @@ impl<T> fmt::Debug for Path<T>
796796
where
797797
T: for<'enc> Encoding<'enc>,
798798
{
799-
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
800-
fmt::Debug::fmt(&self.inner, formatter)
799+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
800+
f.debug_struct("Path")
801+
.field("_encoding", &T::label())
802+
.field("inner", &&self.inner)
803+
.finish()
801804
}
802805
}
803806

src/common/non_utf8/pathbuf.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{
33
borrow::{Borrow, Cow},
44
cmp,
55
collections::TryReserveError,
6+
fmt,
67
hash::{Hash, Hasher},
78
iter::{Extend, FromIterator},
89
marker::PhantomData,
@@ -57,7 +58,6 @@ use std::{
5758
/// ```
5859
///
5960
/// Which method works best depends on what kind of situation you're in.
60-
#[derive(Clone, Debug)]
6161
pub struct PathBuf<T>
6262
where
6363
T: for<'enc> Encoding<'enc>,
@@ -388,6 +388,31 @@ where
388388
}
389389
}
390390

391+
impl<T> Clone for PathBuf<T>
392+
where
393+
T: for<'enc> Encoding<'enc>,
394+
{
395+
#[inline]
396+
fn clone(&self) -> Self {
397+
Self {
398+
_encoding: self._encoding,
399+
inner: self.inner.clone(),
400+
}
401+
}
402+
}
403+
404+
impl<T> fmt::Debug for PathBuf<T>
405+
where
406+
T: for<'enc> Encoding<'enc>,
407+
{
408+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
409+
f.debug_struct("PathBuf")
410+
.field("_encoding", &T::label())
411+
.field("inner", &self.inner)
412+
.finish()
413+
}
414+
}
415+
391416
impl<T> AsRef<[u8]> for PathBuf<T>
392417
where
393418
T: for<'enc> Encoding<'enc>,

src/common/utf8.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub trait Utf8Encoding<'a>: private::Sealed {
1616
/// Represents the type of component that will be derived by this encoding
1717
type Components: Utf8Components<'a>;
1818

19+
/// Static label representing encoding type
20+
fn label() -> &'static str;
21+
1922
/// Produces an iterator of [`Utf8Component`]s over the given the byte slice (`path`)
2023
fn components(path: &'a str) -> Self::Components;
2124

src/common/utf8/path.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,11 @@ impl<T> fmt::Debug for Utf8Path<T>
786786
where
787787
T: for<'enc> Utf8Encoding<'enc>,
788788
{
789-
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
790-
fmt::Debug::fmt(&self.inner, formatter)
789+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
790+
f.debug_struct("Utf8Path")
791+
.field("_encoding", &T::label())
792+
.field("inner", &&self.inner)
793+
.finish()
791794
}
792795
}
793796

src/common/utf8/pathbuf.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ use std::{
6363
/// ```
6464
///
6565
/// Which method works best depends on what kind of situation you're in.
66-
#[derive(Clone, Debug)]
6766
pub struct Utf8PathBuf<T>
6867
where
6968
T: for<'enc> Utf8Encoding<'enc>,
@@ -475,6 +474,31 @@ where
475474
}
476475
}
477476

477+
impl<T> Clone for Utf8PathBuf<T>
478+
where
479+
T: for<'enc> Utf8Encoding<'enc>,
480+
{
481+
#[inline]
482+
fn clone(&self) -> Self {
483+
Self {
484+
_encoding: self._encoding,
485+
inner: self.inner.clone(),
486+
}
487+
}
488+
}
489+
490+
impl<T> fmt::Debug for Utf8PathBuf<T>
491+
where
492+
T: for<'enc> Utf8Encoding<'enc>,
493+
{
494+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
495+
f.debug_struct("Utf8PathBuf")
496+
.field("_encoding", &T::label())
497+
.field("inner", &self.inner)
498+
.finish()
499+
}
500+
}
501+
478502
impl<T> AsRef<[u8]> for Utf8PathBuf<T>
479503
where
480504
T: for<'enc> Utf8Encoding<'enc>,

src/native.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ mod non_utf8 {
2525
/// [`Component`](crate::Component) that is native to the platform during compilation
2626
#[cfg(windows)]
2727
pub type NativeComponent<'a> = crate::windows::WindowsComponent<'a>;
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
#[test]
34+
fn native_path_buf_should_be_cloneable() {
35+
let path = NativePathBuf::from("hello.txt");
36+
assert_eq!(path, path.clone());
37+
}
38+
}
2839
}
2940

3041
mod utf8 {
@@ -51,4 +62,15 @@ mod utf8 {
5162
/// [`Utf8Component`](crate::Utf8Component) that is native to the platform during compilation
5263
#[cfg(windows)]
5364
pub type Utf8NativeComponent<'a> = crate::windows::Utf8WindowsComponent<'a>;
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use super::*;
69+
70+
#[test]
71+
fn utf8_native_path_buf_should_be_cloneable() {
72+
let path = Utf8NativePathBuf::from("hello.txt");
73+
assert_eq!(path, path.clone());
74+
}
75+
}
5476
}

0 commit comments

Comments
 (0)