Skip to content

Commit e7a9463

Browse files
committed
Add impl Display for Id
1 parent b861297 commit e7a9463

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ See the original [`xid`] project for more details.
2222
## Usage
2323

2424
```rust
25-
println!("{}", xid::new().to_string()); //=> bva9lbqn1bt68k8mj62g
25+
println!("{}", xid::new()); //=> bva9lbqn1bt68k8mj62g
2626
```
2727

2828
## Examples

examples/gen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
println!("{}", xid::new().to_string());
2+
println!("{}", xid::new());
33
}

src/id.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
2+
fmt,
23
str::{self, FromStr},
3-
string::ToString,
44
time::{Duration, SystemTime, UNIX_EPOCH},
55
};
66

@@ -54,10 +54,9 @@ impl Id {
5454
}
5555
}
5656

57-
impl ToString for Id {
57+
impl fmt::Display for Id {
5858
// https://github.com/rs/xid/blob/efa678f304ab65d6d57eedcb086798381ae22206/id.go#L208
59-
/// Returns the string representation of the id.
60-
fn to_string(&self) -> String {
59+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6160
let Self(raw) = self;
6261
let mut bs = [0_u8; ENCODED_LEN];
6362
bs[19] = ENC[((raw[11] << 4) & 31) as usize];
@@ -80,7 +79,7 @@ impl ToString for Id {
8079
bs[2] = ENC[((raw[1] >> 1) & 31) as usize];
8180
bs[1] = ENC[(((raw[1] >> 6) | (raw[0] << 2)) & 31) as usize];
8281
bs[0] = ENC[(raw[0] >> 3) as usize];
83-
str::from_utf8(&bs).unwrap().to_string()
82+
write!(f, "{}", str::from_utf8(&bs).expect("valid utf8"))
8483
}
8584
}
8685

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! ## Usage
2020
//!
2121
//! ```
22-
//! println!("{}", xid::new().to_string()); //=> bva9lbqn1bt68k8mj62g
22+
//! println!("{}", xid::new()); //=> bva9lbqn1bt68k8mj62g
2323
//! ```
2424
//!
2525
//! [`xid`]: https://github.com/rs/xid

0 commit comments

Comments
 (0)