//! Efficient decimal integer formatting. //! //! # Safety //! //! This uses `CStr::from_bytes_with_nul_unchecked` and //! `str::from_utf8_unchecked`on the buffer that it filled itself. #![allow(unsafe_code)]
usecrate::backend::fd::{AsFd, AsRawFd}; usecrate::ffi::CStr; use core::fmt::Write; use itoa::{Buffer, Integer}; #[cfg(all(feature = "std", unix))] use std::os::unix::ffi::OsStrExt; #[cfg(all(feature = "std", target_os = "wasi"))] use std::os::wasi::ffi::OsStrExt; #[cfg(feature = "std")] use {core::fmt, std::ffi::OsStr, std::path::Path};
/// Format an integer into a decimal `Path` component, without constructing a /// temporary `PathBuf` or `String`. /// /// This is used for opening paths such as `/proc/self/fd/<fd>` on Linux. /// /// # Examples /// /// ``` /// # #[cfg(any(feature = "fs", feature = "net"))] /// use rustix::path::DecInt; /// /// # #[cfg(any(feature = "fs", feature = "net"))] /// assert_eq!( /// format!("hello {}", DecInt::new(9876).as_ref().display()), /// "hello 9876" /// ); /// ``` #[derive(Clone)] pubstruct DecInt { // 20 `u8`s is enough to hold the decimal ASCII representation of any // `u64`, and we add one for a NUL terminator for `as_c_str`.
buf: [u8; 20 + 1],
len: usize,
}
impl DecInt { /// Construct a new path component from an integer. #[inline] pubfn new<Int: Integer>(i: Int) -> Self { letmut me = DecIntWriter(Self {
buf: [0; 20 + 1],
len: 0,
}); letmut buf = Buffer::new();
me.write_str(buf.format(i)).unwrap();
me.0
}
/// Construct a new path component from a file descriptor. #[inline] pubfn from_fd<Fd: AsFd>(fd: Fd) -> Self { Self::new(fd.as_fd().as_raw_fd())
}
/// Return the raw byte buffer as a `&str`. #[inline] pubfn as_str(&self) -> &str { // SAFETY: `DecInt` always holds a formatted decimal number, so it's // always valid UTF-8. unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
}
/// Return the raw byte buffer as a `&CStr`. #[inline] pubfn as_c_str(&self) -> &CStr { let bytes_with_nul = &self.buf[..=self.len];
debug_assert!(CStr::from_bytes_with_nul(bytes_with_nul).is_ok());
// SAFETY: `self.buf` holds a single decimal ASCII representation and // at least one extra NUL byte. unsafe { CStr::from_bytes_with_nul_unchecked(bytes_with_nul) }
}
/// Return the raw byte buffer. #[inline] pubfn as_bytes(&self) -> &[u8] {
&self.buf[..self.len]
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.