@@ -20,11 +20,38 @@ use sys_common::{FromInner, IntoInner, AsInner};
20
20
/// Unix-specific extensions to `OsString`.
21
21
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
22
22
pub trait OsStringExt {
23
- /// Creates an `OsString` from a byte vector.
23
+ /// Creates an [`OsString`] from a byte vector.
24
+ ///
25
+ /// # Examples
26
+ ///
27
+ /// ```
28
+ /// use std::ffi::OsString;
29
+ /// use std::os::unix::ffi::OsStringExt;
30
+ ///
31
+ /// let bytes = b"foo".to_vec();
32
+ /// let os_string = OsString::from_vec(bytes);
33
+ /// assert_eq!(os_string.to_str(), Some("foo"));
34
+ /// ```
35
+ ///
36
+ /// [`OsString`]: ../../../ffi/struct.OsString.html
24
37
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
25
38
fn from_vec ( vec : Vec < u8 > ) -> Self ;
26
39
27
- /// Yields the underlying byte vector of this `OsString`.
40
+ /// Yields the underlying byte vector of this [`OsString`].
41
+ ///
42
+ /// # Examples
43
+ ///
44
+ /// ```
45
+ /// use std::ffi::OsString;
46
+ /// use std::os::unix::ffi::OsStringExt;
47
+ ///
48
+ /// let mut os_string = OsString::new();
49
+ /// os_string.push("foo");
50
+ /// let bytes = os_string.into_vec();
51
+ /// assert_eq!(bytes, b"foo");
52
+ /// ```
53
+ ///
54
+ /// [`OsString`]: ../../../ffi/struct.OsString.html
28
55
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
29
56
fn into_vec ( self ) -> Vec < u8 > ;
30
57
}
@@ -43,9 +70,36 @@ impl OsStringExt for OsString {
43
70
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
44
71
pub trait OsStrExt {
45
72
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
73
+ /// Creates an [`OsStr`] from a byte slice.
74
+ ///
75
+ /// # Examples
76
+ ///
77
+ /// ```
78
+ /// use std::ffi::OsStr;
79
+ /// use std::os::unix::ffi::OsStrExt;
80
+ ///
81
+ /// let bytes = b"foo";
82
+ /// let os_str = OsStr::from_bytes(bytes);
83
+ /// assert_eq!(os_str.to_str(), Some("foo"));
84
+ /// ```
85
+ ///
86
+ /// [`OsStr`]: ../../../ffi/struct.OsStr.html
46
87
fn from_bytes ( slice : & [ u8 ] ) -> & Self ;
47
88
48
- /// Gets the underlying byte view of the `OsStr` slice.
89
+ /// Gets the underlying byte view of the [`OsStr`] slice.
90
+ ///
91
+ /// # Examples
92
+ ///
93
+ /// ```
94
+ /// use std::ffi::OsStr;
95
+ /// use std::os::unix::ffi::OsStrExt;
96
+ ///
97
+ /// let mut os_str = OsStr::new("foo");
98
+ /// let bytes = os_str.as_bytes();
99
+ /// assert_eq!(bytes, b"foo");
100
+ /// ```
101
+ ///
102
+ /// [`OsStr`]: ../../../ffi/struct.OsStr.html
49
103
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
50
104
fn as_bytes ( & self ) -> & [ u8 ] ;
51
105
}
0 commit comments