diff --git a/README.md b/README.md index 0320cc8..d11e8a6 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ See the original [`xid`] project for more details. ## Usage ```rust -println!("{}", xid::new().to_string()); //=> bva9lbqn1bt68k8mj62g +println!("{}", xid::new()); //=> bva9lbqn1bt68k8mj62g ``` ## Examples diff --git a/examples/gen.rs b/examples/gen.rs index fa22fdc..1025607 100644 --- a/examples/gen.rs +++ b/examples/gen.rs @@ -1,3 +1,3 @@ fn main() { - println!("{}", xid::new().to_string()); + println!("{}", xid::new()); } diff --git a/src/generator.rs b/src/generator.rs index ef9be79..4db24e6 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -33,12 +33,13 @@ impl Generator { self.with_time(&SystemTime::now()) } + #[cfg_attr(target_os = "windows", allow(clippy::trivially_copy_pass_by_ref))] fn with_time(&self, time: &SystemTime) -> Id { // Panic if the time is before the epoch. let unix_ts = time .duration_since(UNIX_EPOCH) .expect("Clock may have gone backwards"); - #[allow(clippy::clippy::cast_possible_truncation)] + #[allow(clippy::cast_possible_truncation)] self.generate(unix_ts.as_secs() as u32) } diff --git a/src/id.rs b/src/id.rs index 6d3f738..c6800db 100644 --- a/src/id.rs +++ b/src/id.rs @@ -1,6 +1,6 @@ use std::{ + fmt, str::{self, FromStr}, - string::ToString, time::{Duration, SystemTime, UNIX_EPOCH}, }; @@ -54,10 +54,9 @@ impl Id { } } -impl ToString for Id { +impl fmt::Display for Id { // https://github.com/rs/xid/blob/efa678f304ab65d6d57eedcb086798381ae22206/id.go#L208 - /// Returns the string representation of the id. - fn to_string(&self) -> String { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Self(raw) = self; let mut bs = [0_u8; ENCODED_LEN]; bs[19] = ENC[((raw[11] << 4) & 31) as usize]; @@ -80,7 +79,7 @@ impl ToString for Id { bs[2] = ENC[((raw[1] >> 1) & 31) as usize]; bs[1] = ENC[(((raw[1] >> 6) | (raw[0] << 2)) & 31) as usize]; bs[0] = ENC[(raw[0] >> 3) as usize]; - str::from_utf8(&bs).unwrap().to_string() + write!(f, "{}", str::from_utf8(&bs).expect("valid utf8")) } } diff --git a/src/lib.rs b/src/lib.rs index c4ad440..67717cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ //! ## Usage //! //! ``` -//! println!("{}", xid::new().to_string()); //=> bva9lbqn1bt68k8mj62g +//! println!("{}", xid::new()); //=> bva9lbqn1bt68k8mj62g //! ``` //! //! [`xid`]: https://github.com/rs/xid diff --git a/src/pid.rs b/src/pid.rs index 80d0a2f..d6b1a4b 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -4,7 +4,7 @@ use crc32fast::Hasher; // 2 bytes of PID // https://github.com/rs/xid/blob/efa678f304ab65d6d57eedcb086798381ae22206/id.go#L159 -#[allow(clippy::clippy::cast_possible_truncation)] +#[allow(clippy::cast_possible_truncation)] pub fn get() -> u16 { // https://github.com/rs/xid/blob/efa678f304ab65d6d57eedcb086798381ae22206/id.go#L105 // > If /proc/self/cpuset exists and is not /, we can assume that we are in a