@@ -7,6 +7,7 @@ use std::u8;
7
7
8
8
use crate :: os_str_into_bstr;
9
9
use crate :: try_from_bstr;
10
+ use crate :: try_from_byte_slice;
10
11
11
12
/// A wrapper for `BStr`. It is used to enforce the following constraints:
12
13
///
@@ -19,6 +20,14 @@ pub struct RelativePath {
19
20
}
20
21
21
22
impl RelativePath {
23
+ fn new_unchecked ( value : & BStr ) -> Result < & RelativePath , Error > {
24
+ // SAFETY: `RelativePath` is transparent and equivalent to a `&BStr` if provided as reference.
25
+ #[ allow( unsafe_code) ]
26
+ unsafe {
27
+ std:: mem:: transmute ( value)
28
+ }
29
+ }
30
+
22
31
/// TODO
23
32
/// Needs docs.
24
33
pub fn ends_with ( & self , needle : & [ u8 ] ) -> bool {
@@ -49,24 +58,44 @@ impl<'a> TryFrom<&'a BStr> for &'a RelativePath {
49
58
gix_validate:: path:: component ( component, None , options) ?;
50
59
}
51
60
52
- todo ! ( )
61
+ RelativePath :: new_unchecked ( value )
53
62
}
54
63
}
55
64
56
65
impl < ' a , const N : usize > TryFrom < & ' a [ u8 ; N ] > for & ' a RelativePath {
57
66
type Error = Error ;
58
67
59
68
#[ inline]
60
- fn try_from ( _value : & ' a [ u8 ; N ] ) -> Result < Self , Self :: Error > {
61
- todo ! ( )
69
+ fn try_from ( value : & ' a [ u8 ; N ] ) -> Result < Self , Self :: Error > {
70
+ let path: & std:: path:: Path = & try_from_byte_slice ( value) ?;
71
+
72
+ let options: Options = Default :: default ( ) ;
73
+
74
+ for component in path. components ( ) {
75
+ let component = os_str_into_bstr ( component. as_os_str ( ) ) ?;
76
+
77
+ gix_validate:: path:: component ( component, None , options) ?;
78
+ }
79
+
80
+ RelativePath :: new_unchecked ( value. into ( ) )
62
81
}
63
82
}
64
83
65
- impl TryFrom < BString > for & RelativePath {
84
+ impl < ' a > TryFrom < & ' a BString > for & ' a RelativePath {
66
85
type Error = Error ;
67
86
68
- fn try_from ( _value : BString ) -> Result < Self , Self :: Error > {
69
- todo ! ( )
87
+ fn try_from ( value : & ' a BString ) -> Result < Self , Self :: Error > {
88
+ let path: & std:: path:: Path = & try_from_bstr ( value. as_bstr ( ) ) ?;
89
+
90
+ let options: Options = Default :: default ( ) ;
91
+
92
+ for component in path. components ( ) {
93
+ let component = os_str_into_bstr ( component. as_os_str ( ) ) ?;
94
+
95
+ gix_validate:: path:: component ( component, None , options) ?;
96
+ }
97
+
98
+ RelativePath :: new_unchecked ( value. as_bstr ( ) )
70
99
}
71
100
}
72
101
0 commit comments