Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add impl Display for Id #5

Merged
merged 3 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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