File tree Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -741,8 +741,17 @@ impl SockAddr {
741
741
}
742
742
}
743
743
744
- /// Returns this address as a `Path` reference if it is an `AF_UNIX` pathname address, otherwise
745
- /// returns `None`.
744
+ /// Returns this address as Unix `SocketAddr` if it is an `AF_UNIX` pathname
745
+ /// address, otherwise returns `None`.
746
+ pub fn as_unix ( & self ) -> Option < std:: os:: unix:: net:: SocketAddr > {
747
+ let path = self . as_pathname ( ) ?;
748
+ // SAFETY: we can represent this as a valid pathname, then so can the
749
+ // standard library.
750
+ Some ( std:: os:: unix:: net:: SocketAddr :: from_pathname ( path) . unwrap ( ) )
751
+ }
752
+
753
+ /// Returns this address as a `Path` reference if it is an `AF_UNIX`
754
+ /// pathname address, otherwise returns `None`.
746
755
pub fn as_pathname ( & self ) -> Option < & Path > {
747
756
self . as_sockaddr_un ( ) . and_then ( |storage| {
748
757
( self . len ( ) > offset_of_path ( storage) as u32 && storage. sun_path [ 0 ] != 0 ) . then ( || {
Original file line number Diff line number Diff line change @@ -155,6 +155,8 @@ fn socket_address_unix() {
155
155
assert ! ( !addr. is_unnamed( ) ) ;
156
156
assert_eq ! ( addr. as_pathname( ) , Some ( Path :: new( string) ) ) ;
157
157
assert_eq ! ( addr. as_abstract_namespace( ) , None ) ;
158
+ let unix = addr. as_unix ( ) . unwrap ( ) ;
159
+ assert_eq ! ( addr. as_pathname( ) , unix. as_pathname( ) ) ;
158
160
}
159
161
}
160
162
@@ -172,6 +174,7 @@ fn socket_address_unix_unnamed() {
172
174
assert ! ( addr. is_unnamed( ) ) ;
173
175
assert_eq ! ( addr. as_pathname( ) , None ) ;
174
176
assert_eq ! ( addr. as_abstract_namespace( ) , None ) ;
177
+ assert ! ( addr. as_unix( ) . is_none( ) ) ;
175
178
}
176
179
}
177
180
You can’t perform that action at this time.
0 commit comments