@@ -5,7 +5,7 @@ use crate::sys::time::TimeVal;
5
5
use crate :: Result ;
6
6
use cfg_if:: cfg_if;
7
7
use libc:: { self , c_int, c_void, socklen_t} ;
8
- use std:: ffi:: { OsStr , OsString } ;
8
+ use std:: ffi:: { CStr , CString , OsStr , OsString } ;
9
9
use std:: mem:: { self , MaybeUninit } ;
10
10
use std:: os:: unix:: ffi:: OsStrExt ;
11
11
use std:: os:: unix:: io:: { AsFd , AsRawFd } ;
@@ -1073,8 +1073,8 @@ sockopt_impl!(
1073
1073
GetOnly ,
1074
1074
libc:: SYSPROTO_CONTROL ,
1075
1075
libc:: UTUN_OPT_IFNAME ,
1076
- OsString ,
1077
- GetOsString <[ u8 ; libc:: IFNAMSIZ ] >
1076
+ CString ,
1077
+ GetCString <[ u8 ; libc:: IFNAMSIZ ] >
1078
1078
) ;
1079
1079
1080
1080
#[ allow( missing_docs) ]
@@ -1579,3 +1579,32 @@ impl<'a> Set<'a, OsString> for SetOsString<'a> {
1579
1579
}
1580
1580
}
1581
1581
1582
+ /// Getter for a `CString` value.
1583
+ struct GetCString < T : AsMut < [ u8 ] > > {
1584
+ len : socklen_t ,
1585
+ val : MaybeUninit < T > ,
1586
+ }
1587
+
1588
+ impl < T : AsMut < [ u8 ] > > Get < CString > for GetCString < T > {
1589
+ fn uninit ( ) -> Self {
1590
+ GetCString {
1591
+ len : mem:: size_of :: < T > ( ) as socklen_t ,
1592
+ val : MaybeUninit :: uninit ( ) ,
1593
+ }
1594
+ }
1595
+
1596
+ fn ffi_ptr ( & mut self ) -> * mut c_void {
1597
+ self . val . as_mut_ptr ( ) . cast ( )
1598
+ }
1599
+
1600
+ fn ffi_len ( & mut self ) -> * mut socklen_t {
1601
+ & mut self . len
1602
+ }
1603
+
1604
+ unsafe fn assume_init ( self ) -> CString {
1605
+ let mut v = unsafe { self . val . assume_init ( ) } ;
1606
+ CStr :: from_bytes_until_nul ( v. as_mut ( ) )
1607
+ . expect ( "string should be null-terminated" )
1608
+ . to_owned ( )
1609
+ }
1610
+ }
0 commit comments