Skip to content

Commit d24acd1

Browse files
committed
lint: Gate internal-only traits to when they are really used
This is one way of dealing with warnings shown otherwise.
1 parent a424472 commit d24acd1

File tree

2 files changed

+51
-39
lines changed

2 files changed

+51
-39
lines changed

src/helpers.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -61,49 +61,55 @@ impl PointerToCStr for *const i8 {
6161
}
6262
}
6363

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+
}
7982

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.
8184

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 {})?;
8992

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+
}
9397
}
94-
}
9598

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+
}
100104
}
101-
}
102105

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::*;

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,10 @@ pub mod led;
184184
#[cfg(riot_module_auto_init)]
185185
pub mod auto_init;
186186

187+
// Gated like socket_embedded_nal_async_udp as it is only used there -- expand as needed.
188+
#[cfg(all(
189+
riot_module_sock_udp,
190+
riot_module_sock_aux_local,
191+
feature = "with_embedded_nal_async"
192+
))]
187193
mod async_helpers;

0 commit comments

Comments
 (0)