@@ -61,49 +61,55 @@ impl PointerToCStr for *const i8 {
61
61
}
62
62
}
63
63
64
- /// Trait that eases conversions from a char slice (no matter the signedness, they are used
65
- /// inconsistently in RIOT) to a CStr. The result is often used with `?.to_str().ok()?`.
66
- pub ( crate ) trait SliceToCStr {
67
- /// Cast self around until it is suitable input to [`core::ffi::CStr::from_bytes_until_nul()`],
68
- /// and run that function.
69
- ///
70
- /// Note that while "the slice until any null byte" could be safely used in Rust (as a slice or
71
- /// even a str), its presence in C practically always indicates an error, also because that
72
- /// data wouldn't be usable by other C code using its string conventions.
73
- ///
74
- /// It is using a local error type because while the semantics of `from_bytes_until_nul` are
75
- /// the right ones considering how this is used on C fields that are treated with `strlen()`
76
- /// etc., that function is not stable yet and emulated.
77
- fn to_cstr ( & self ) -> Result < & core:: ffi:: CStr , FromBytesUntilNulError > ;
78
- }
64
+ // Gated because it is only used there -- expand as needed.
65
+ #[ cfg( riot_module_vfs) ]
66
+ mod slice_to_cstr {
67
+ /// Trait that eases conversions from a char slice (no matter the signedness, they are used
68
+ /// inconsistently in RIOT) to a CStr. The result is often used with `?.to_str().ok()?`.
69
+ pub ( crate ) trait SliceToCStr {
70
+ /// Cast self around until it is suitable input to [`core::ffi::CStr::from_bytes_until_nul()`],
71
+ /// and run that function.
72
+ ///
73
+ /// Note that while "the slice until any null byte" could be safely used in Rust (as a slice or
74
+ /// even a str), its presence in C practically always indicates an error, also because that
75
+ /// data wouldn't be usable by other C code using its string conventions.
76
+ ///
77
+ /// It is using a local error type because while the semantics of `from_bytes_until_nul` are
78
+ /// the right ones considering how this is used on C fields that are treated with `strlen()`
79
+ /// etc., that function is not stable yet and emulated.
80
+ fn to_cstr ( & self ) -> Result < & core:: ffi:: CStr , FromBytesUntilNulError > ;
81
+ }
79
82
80
- // Unlike in the from_ptr case, this is consistently taking u8, so only the i8 case gets casting.
83
+ // Unlike in the from_ptr case, this is consistently taking u8, so only the i8 case gets casting.
81
84
82
- impl SliceToCStr for [ u8 ] {
83
- fn to_cstr ( & self ) -> Result < & core:: ffi:: CStr , FromBytesUntilNulError > {
84
- // Emulate from_bytes_until_null
85
- let index = self
86
- . iter ( )
87
- . position ( |& c| c == 0 )
88
- . ok_or ( FromBytesUntilNulError { } ) ?;
85
+ impl SliceToCStr for [ u8 ] {
86
+ fn to_cstr ( & self ) -> Result < & core:: ffi:: CStr , FromBytesUntilNulError > {
87
+ // Emulate from_bytes_until_null
88
+ let index = self
89
+ . iter ( )
90
+ . position ( |& c| c == 0 )
91
+ . ok_or ( FromBytesUntilNulError { } ) ?;
89
92
90
- core:: ffi:: CStr :: from_bytes_with_nul ( & self [ ..index + 1 ] )
91
- // Actually the error is unreachable
92
- . map_err ( |_| FromBytesUntilNulError { } )
93
+ core:: ffi:: CStr :: from_bytes_with_nul ( & self [ ..index + 1 ] )
94
+ // Actually the error is unreachable
95
+ . map_err ( |_| FromBytesUntilNulError { } )
96
+ }
93
97
}
94
- }
95
98
96
- impl SliceToCStr for [ i8 ] {
97
- fn to_cstr ( & self ) -> Result < & core:: ffi:: CStr , FromBytesUntilNulError > {
98
- let s: & [ u8 ] = unsafe { core:: mem:: transmute ( self ) } ;
99
- s. to_cstr ( )
99
+ impl SliceToCStr for [ i8 ] {
100
+ fn to_cstr ( & self ) -> Result < & core:: ffi:: CStr , FromBytesUntilNulError > {
101
+ let s: & [ u8 ] = unsafe { core:: mem:: transmute ( self ) } ;
102
+ s. to_cstr ( )
103
+ }
100
104
}
101
- }
102
105
103
- /// Error from [SliceToCStr::to_cstr].
104
- ///
105
- /// This will become [core::ffi:FromBytesUntilNulError] once that's stable, and may be changed
106
- /// without a breaking release even though it's technically a breaking change. (At this point, that
107
- /// type will be `pub use`d here and deprecated).
108
- #[ derive( Debug ) ]
109
- pub struct FromBytesUntilNulError { }
106
+ /// Error from [SliceToCStr::to_cstr].
107
+ ///
108
+ /// This will become [core::ffi:FromBytesUntilNulError] once that's stable, and may be changed
109
+ /// without a breaking release even though it's technically a breaking change. (At this point, that
110
+ /// type will be `pub use`d here and deprecated).
111
+ #[ derive( Debug ) ]
112
+ pub struct FromBytesUntilNulError { }
113
+ }
114
+ #[ cfg( riot_module_vfs) ]
115
+ pub use slice_to_cstr:: * ;
0 commit comments