https://github.com/not-nik/rustflakes
An implementation of Discord's version of Twitter's snowflakes in Rust.
https://github.com/not-nik/rustflakes
discord rust snowflake snowflake-twitter
Last synced: 3 months ago
JSON representation
An implementation of Discord's version of Twitter's snowflakes in Rust.
- Host: GitHub
- URL: https://github.com/not-nik/rustflakes
- Owner: Not-Nik
- License: mit
- Created: 2020-08-09T14:38:43.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-30T09:56:58.000Z (almost 6 years ago)
- Last Synced: 2025-03-18T05:08:57.078Z (over 1 year ago)
- Topics: discord, rust, snowflake, snowflake-twitter
- Language: Rust
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rustflakes
An implementation of Discord's version of Twitter's snowflakes in Rust.
## Usage
```rust
use rustflakes::SnowflakeWorker;
fn main() {
let worker_id = 1;
let process_id = 2;
let mut worker = SnowflakeWorker::new(worker_id, process_id);
let snowflake = Snowflake::from(worker.make());
println!("Created snowflake {}", snowflake.flake);
println!("Snowflake created at: {}", snowflake.timestamp);
println!("Snowflake created by worker {}", snowflake.worker_id);
println!("Snowflake created by process {}", snowflake.process_id);
println!("Snowflakes increment is {}", snowflake.increment);
}
```
## Structure
Snowflakes are structured as defined in the [Discord API documentation](https://discord.com/developers/docs/reference#snowflakes-snowflake-id-format-structure-left-to-right).
Field | Bits | Number of Bits | Description
--------------------|----------|----------------|------------
Timestamp | 63 to 22 | 42 bits | Milliseconds since Discord Epoch, the first second of 2015 or 1420070400000.
Internal worker ID | 21 to 17 | 5 bits |
Internal process ID | 16 to 12 | 5 bits |
Increment | 11 to 0 | 12 bits | For every ID that is generated on that process, this number is incremented