Skip to content

Commit

Permalink
Merge pull request #5 from kazk/impl-display
Browse files Browse the repository at this point in the history
  • Loading branch information
kazk authored Jun 24, 2022
2 parents b861297 + 38c8537 commit 1f0c734
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/gen.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
println!("{}", xid::new().to_string());
println!("{}", xid::new());
}
3 changes: 2 additions & 1 deletion src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
9 changes: 4 additions & 5 deletions src/id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fmt,
str::{self, FromStr},
string::ToString,
time::{Duration, SystemTime, UNIX_EPOCH},
};

Expand Down Expand Up @@ -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];
Expand All @@ -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"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! ## Usage
//!
//! ```
//! println!("{}", xid::new().to_string()); //=> bva9lbqn1bt68k8mj62g
//! println!("{}", xid::new()); //=> bva9lbqn1bt68k8mj62g
//! ```
//!
//! [`xid`]: https://github.com/rs/xid
Expand Down
2 changes: 1 addition & 1 deletion src/pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f0c734

Please sign in to comment.