Skip to content

Commit c97143c

Browse files
authored
[byteorder] Add cfg no_fp_fmt_parse (#3429)
This cfg deactivates Debug and Display for floatting point numbers, this is particluarly useful in kernel context. The implementation is inspired by: rust-lang/rust@ec7292ad3c35 Fixes #3426
1 parent bc0bb77 commit c97143c

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

zerocopy/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ fn main() {
9595
println!("cargo:rustc-check-cfg=cfg(coverage_nightly)");
9696
println!("cargo:rustc-check-cfg=cfg(zerocopy_inline_always)");
9797
println!("cargo:rustc-check-cfg=cfg(zerocopy_unstable_ptr)");
98+
println!("cargo:rustc-check-cfg=cfg(no_fp_fmt_parse)");
9899
}
99100

100101
for version_cfg in version_cfgs {

zerocopy/src/byteorder.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,42 @@ pub type BE = BigEndian;
162162
/// A type alias for [`LittleEndian`].
163163
pub type LE = LittleEndian;
164164

165+
macro_rules! impl_dbg_trait {
166+
($name:ident, $native:ident) => {
167+
impl<O: ByteOrder> Debug for $name<O> {
168+
#[inline]
169+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
170+
// This results in a format like "U16(42)".
171+
f.debug_tuple(stringify!($name)).field(&self.get()).finish()
172+
}
173+
}
174+
};
175+
}
176+
177+
macro_rules! impl_dbg_traits {
178+
($name:ident, $native:ident, "floating point number") => {
179+
#[cfg(not(no_fp_fmt_parse))]
180+
impl_dbg_trait!($name, $native);
181+
182+
#[cfg(no_fp_fmt_parse)]
183+
impl<O: ByteOrder> Debug for $name<O> {
184+
#[inline]
185+
fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result {
186+
panic!("floating point support is turned off");
187+
}
188+
}
189+
};
190+
($name:ident, $native:ident, "unsigned integer") => {
191+
impl_dbg_traits!($name, $native, @all_types);
192+
};
193+
($name:ident, $native:ident, "signed integer") => {
194+
impl_dbg_traits!($name, $native, @all_types);
195+
};
196+
($name:ident, $native:ident, @all_types) => {
197+
impl_dbg_trait!($name, $native);
198+
};
199+
}
200+
165201
macro_rules! impl_fmt_trait {
166202
($name:ident, $native:ident, $trait:ident) => {
167203
impl<O: ByteOrder> $trait for $name<O> {
@@ -175,6 +211,7 @@ macro_rules! impl_fmt_trait {
175211

176212
macro_rules! impl_fmt_traits {
177213
($name:ident, $native:ident, "floating point number") => {
214+
#[cfg(not(no_fp_fmt_parse))]
178215
impl_fmt_trait!($name, $native, Display);
179216
};
180217
($name:ident, $native:ident, "unsigned integer") => {
@@ -686,16 +723,9 @@ example of how it can be used for parsing UDP packets.
686723
}
687724
}
688725

726+
impl_dbg_traits!($name, $native, $number_kind);
689727
impl_fmt_traits!($name, $native, $number_kind);
690728
impl_ops_traits!($name, $native, $number_kind);
691-
692-
impl<O: ByteOrder> Debug for $name<O> {
693-
#[inline]
694-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
695-
// This results in a format like "U16(42)".
696-
f.debug_tuple(stringify!($name)).field(&self.get()).finish()
697-
}
698-
}
699729
};
700730
}
701731

0 commit comments

Comments
 (0)