typedflake 0.2.0

Snowflake-style IDs as distinct, validated newtypes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! ID generation handles.

use core::fmt;
use core::marker::PhantomData;
use std::sync::Arc;
use std::thread;
use std::time::SystemTimeError;

use crate::clock;
use crate::global;
use crate::id::{self, Id};
use crate::node::{Node, NodeError};
use crate::state::{self, State};

/// Generates IDs of type `I` for one node.
///
/// Obtain one from [`Id::generator`]. Cloning is cheap, and every generator
/// for the same ID type and node shares one state, so no two of them can issue
/// the same ID.
///
/// ```
/// use typedflake::typedflake;
///
/// #[typedflake(epoch = "2025-01-01")]
/// pub struct UserId(i64);
///
/// let generator = UserId::generator(17)?;
/// let first = generator.generate()?;
/// let second = generator.clone().generate()?;
///
/// assert!(first < second);
/// assert_eq!(first.parts().node, 17);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Generator<I: Id> {
    state: Arc<State>,
    /// Packed node, already shifted into its position in the ID.
    node: u64,
    _id: PhantomData<fn() -> I>,
}

impl<I: Id> Generator<I> {
    pub(crate) fn new(node: I::Node) -> Result<Self, GeneratorError> {
        let layout = I::__LAYOUT;
        let packed = node
            .pack(layout.node_bits)
            .map_err(GeneratorError::InvalidNode)?;

        Ok(Self {
            state: state::shared::<I>(packed),
            node: packed << layout.node_shift,
            _id: PhantomData,
        })
    }

    /// Returns the node this generator issues IDs for.
    pub fn node(&self) -> I::Node {
        let layout = I::__LAYOUT;
        I::Node::unpack(self.node >> layout.node_shift, layout.node_bits)
    }

    /// Generates an ID.
    ///
    /// Returns immediately. If this millisecond's sequence is used up it
    /// returns [`GenerateError::SequenceExhausted`] instead of waiting.
    pub fn generate(&self) -> Result<I, GenerateError> {
        let layout = I::__LAYOUT;
        let (timestamp, sequence) = self.state.next(&layout, || {
            let now_unix_millis = clock::unix_millis().map_err(GenerateError::Clock)?;
            now_unix_millis
                .checked_sub(layout.epoch)
                .ok_or(GenerateError::ClockBeforeEpoch {
                    now_unix_millis,
                    epoch_unix_millis: layout.epoch,
                })
        })?;
        Ok(id::from_valid_bits(
            (timestamp << layout.timestamp_shift) | self.node | sequence,
        ))
    }

    /// Generates an ID, blocking the thread while it waits for sequence
    /// capacity.
    ///
    /// Only sequence exhaustion is retried; every other error returns at once.
    /// Capacity returns with the next millisecond.
    pub fn generate_blocking(&self) -> Result<I, GenerateError> {
        loop {
            match self.generate() {
                Err(GenerateError::SequenceExhausted) => {}
                result => return result,
            }
            thread::sleep(clock::until_next_milli());
        }
    }

    /// Generates an ID, waiting for sequence capacity on a Tokio timer.
    ///
    /// Only sequence exhaustion is retried, and the timer is used only when
    /// waiting is needed. Capacity returns with the next millisecond; to bound
    /// the wait, wrap the call in `tokio::time::timeout`.
    #[cfg(feature = "tokio")]
    pub async fn generate_async(&self) -> Result<I, GenerateError> {
        loop {
            match self.generate() {
                Err(GenerateError::SequenceExhausted) => {}
                result => return result,
            }
            tokio::time::sleep(clock::until_next_milli()).await;
        }
    }
}

impl<I: Id> Clone for Generator<I> {
    fn clone(&self) -> Self {
        Self {
            state: Arc::clone(&self.state),
            node: self.node,
            _id: PhantomData,
        }
    }
}

impl<I: Id> fmt::Debug for Generator<I> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Generator")
            .field("id", &core::any::type_name::<I>())
            .field("node", &(self.node >> I::__LAYOUT.node_shift))
            .finish()
    }
}

/// Returns the generator behind `I::generate()`, resolving it from the global
/// default node of its type on first use. A failure leaves it unresolved, so
/// generating before [`init`](crate::init) does not freeze anything.
pub(crate) fn default_generator<I: Id>() -> Result<&'static Generator<I>, GenerateError> {
    let cell = I::__default_generator();
    if let Some(generator) = cell.get() {
        return Ok(generator);
    }

    let node = global::default_node::<I::Node>()?;
    let generator = Generator::new(node).map_err(GenerateError::DefaultGenerator)?;
    Ok(cell.get_or_init(|| generator))
}

/// A generator could not be created for a node.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GeneratorError {
    /// The node does not fit the ID's format.
    InvalidNode(NodeError),
}

impl fmt::Display for GeneratorError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidNode(_) => f.write_str("node does not fit the ID format"),
        }
    }
}

impl std::error::Error for GeneratorError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::InvalidNode(error) => Some(error),
        }
    }
}

/// An ID could not be generated.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum GenerateError {
    /// [`init`](crate::init) has not been called with a node of the type this
    /// ID uses.
    NotInitialized {
        /// Node type of the ID: `u32`, or its `TypedNode` struct.
        node: &'static str,
    },
    /// The default node does not fit this ID's format.
    DefaultGenerator(GeneratorError),
    /// The system clock could not be read.
    Clock(SystemTimeError),
    /// The system clock is before the format's epoch.
    ClockBeforeEpoch {
        /// Current time, in milliseconds since the Unix epoch.
        now_unix_millis: u64,
        /// The format's epoch, in milliseconds since the Unix epoch.
        epoch_unix_millis: u64,
    },
    /// The system clock moved behind the last issued timestamp. Generation
    /// resumes on its own once the clock catches up.
    ClockRollback {
        /// How far the clock is behind the last issued timestamp.
        behind_millis: u64,
    },
    /// Every sequence number of the current millisecond is used. Retrying on
    /// the next millisecond succeeds.
    SequenceExhausted,
    /// The current time no longer fits the format's timestamp width.
    TimestampExhausted,
}

impl fmt::Display for GenerateError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotInitialized { node } => write!(
                f,
                "no default `{node}` node: call `typedflake::init` with one during startup"
            ),
            Self::DefaultGenerator(_) => f.write_str("default node is not valid for this ID"),
            Self::Clock(_) => f.write_str("system clock could not be read"),
            Self::ClockBeforeEpoch {
                now_unix_millis,
                epoch_unix_millis,
            } => write!(
                f,
                "system clock ({now_unix_millis} ms) is before the ID epoch ({epoch_unix_millis} ms)"
            ),
            Self::ClockRollback { behind_millis } => write!(
                f,
                "system clock is {behind_millis} ms behind the last generated ID"
            ),
            Self::SequenceExhausted => {
                f.write_str("sequence exhausted for the current millisecond")
            }
            Self::TimestampExhausted => {
                f.write_str("current time no longer fits the ID's timestamp bits")
            }
        }
    }
}

impl std::error::Error for GenerateError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::DefaultGenerator(error) => Some(error),
            Self::Clock(error) => Some(error),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use typedflake::typedflake;

    use super::*;
    use crate::Parts;
    use crate::clock::mock;

    const EPOCH: u64 = 1_735_689_600_000;

    #[test]
    fn ids_encode_time_node_and_sequence() {
        #[typedflake(epoch = "2025-01-01")]
        struct TestId(i64);

        let clock = mock::freeze(EPOCH + 1_000);
        let generator = TestId::generator(17).unwrap();

        let first = generator.generate().unwrap();
        let second = generator.generate().unwrap();
        clock.set(EPOCH + 1_001);
        let third = generator.generate().unwrap();

        let parts = |elapsed_millis, sequence| Parts {
            elapsed_millis,
            node: 17,
            sequence,
        };
        assert_eq!(first.parts(), parts(1_000, 0));
        assert_eq!(second.parts(), parts(1_000, 1));
        assert_eq!(third.parts(), parts(1_001, 0));
        assert_eq!(first.get(), (1_000 << 22) | (17 << 12));
        assert_eq!(first.unix_millis(), Ok(EPOCH + 1_000));
        assert!(first < second && second < third);
    }

    #[test]
    fn clock_before_epoch_is_an_error() {
        #[typedflake(epoch = "2025-01-01")]
        struct TestId(i64);

        let _clock = mock::freeze(EPOCH - 1);
        let generator = TestId::generator(0).unwrap();

        assert!(matches!(
            generator.generate(),
            Err(GenerateError::ClockBeforeEpoch {
                now_unix_millis,
                epoch_unix_millis: EPOCH,
            }) if now_unix_millis == EPOCH - 1
        ));
    }

    #[test]
    fn rollback_is_reported_and_generation_recovers() {
        #[typedflake(epoch = "2025-01-01")]
        struct TestId(i64);

        let clock = mock::freeze(EPOCH + 500);
        let generator = TestId::generator(1).unwrap();
        let before = generator.generate().unwrap();

        clock.set(EPOCH + 480);
        assert!(matches!(
            generator.generate(),
            Err(GenerateError::ClockRollback { behind_millis: 20 })
        ));
        // Waiting entry points do not retry a rollback.
        assert!(matches!(
            generator.generate_blocking(),
            Err(GenerateError::ClockRollback { behind_millis: 20 })
        ));

        clock.set(EPOCH + 500);
        let after = generator.generate().unwrap();
        assert_eq!(after.parts().sequence, before.parts().sequence + 1);
    }

    #[test]
    fn sequence_exhaustion_is_retryable() {
        #[typedflake(epoch = "2025-01-01", bits(timestamp = 41, node = 10, sequence = 2))]
        struct TestId(i64);

        let clock = mock::freeze(EPOCH + 1);
        let generator = TestId::generator(3).unwrap();
        for _ in 0..4 {
            generator.generate().unwrap();
        }

        assert!(matches!(
            generator.generate(),
            Err(GenerateError::SequenceExhausted)
        ));
        clock.set(EPOCH + 2);
        assert_eq!(generator.generate().unwrap().parts().sequence, 0);
    }

    #[test]
    fn timestamp_exhaustion_is_permanent() {
        #[typedflake(epoch = "2025-01-01", bits(timestamp = 4, node = 10, sequence = 12))]
        struct TestId(i64);

        let clock = mock::freeze(EPOCH + 15);
        let generator = TestId::generator(0).unwrap();
        assert_eq!(generator.generate().unwrap().parts().elapsed_millis, 15);

        clock.set(EPOCH + 16);
        assert!(matches!(
            generator.generate(),
            Err(GenerateError::TimestampExhausted)
        ));
        assert!(matches!(
            generator.generate_blocking(),
            Err(GenerateError::TimestampExhausted)
        ));
    }

    #[test]
    fn generators_for_the_same_node_share_state() {
        #[typedflake(epoch = "2025-01-01")]
        struct TestId(i64);
        #[typedflake(epoch = "2025-01-01")]
        struct OtherId(i64);

        let _clock = mock::freeze(EPOCH + 7);
        let first = TestId::generator(1).unwrap();
        let again = TestId::generator(1).unwrap();
        let cloned = first.clone();
        let other_node = TestId::generator(2).unwrap();
        let other_type = OtherId::generator(1).unwrap();

        assert_eq!(first.generate().unwrap().parts().sequence, 0);
        assert_eq!(again.generate().unwrap().parts().sequence, 1);
        assert_eq!(cloned.generate().unwrap().parts().sequence, 2);
        assert_eq!(other_node.generate().unwrap().parts().sequence, 0);
        assert_eq!(other_type.generate().unwrap().parts().sequence, 0);
        assert_eq!(again.node(), 1);
        assert_eq!(other_node.node(), 2);
    }

    #[test]
    fn out_of_range_node_is_rejected_without_truncation() {
        #[typedflake(epoch = "2025-01-01")]
        struct TestId(i64);
        #[typedflake(epoch = "2025-01-01", bits(timestamp = 41, node = 0, sequence = 12))]
        struct NodelessId(i64);

        assert_eq!(
            TestId::generator(1024).unwrap_err(),
            GeneratorError::InvalidNode(NodeError::new("node", 1024, 1023))
        );
        assert!(TestId::generator(1023).is_ok());
        assert_eq!(
            NodelessId::generator(1).unwrap_err(),
            GeneratorError::InvalidNode(NodeError::new("node", 1, 0))
        );
        assert!(NodelessId::generator(0).is_ok());
    }
}

#[cfg(all(test, feature = "tokio"))]
mod tokio_tests {
    use std::time::Duration;

    use typedflake::typedflake;

    use super::*;
    use crate::clock::mock;

    const EPOCH: u64 = 1_735_689_600_000;

    /// With the clock frozen, capacity never returns. A wait built on a
    /// blocking sleep would hang here instead of letting the timeout fire.
    #[tokio::test(flavor = "current_thread", start_paused = true)]
    async fn async_wait_yields_to_the_runtime_and_can_be_cancelled() {
        #[typedflake(epoch = "2025-01-01", bits(timestamp = 41, node = 10, sequence = 1))]
        struct TestId(i64);

        let _clock = mock::freeze(EPOCH + 1);
        let generator = TestId::generator(0).unwrap();
        generator.generate_async().await.unwrap();
        generator.generate_async().await.unwrap();

        let waited =
            tokio::time::timeout(Duration::from_millis(10), generator.generate_async()).await;
        assert!(waited.is_err());
    }

    #[tokio::test(flavor = "current_thread", start_paused = true)]
    async fn async_wait_does_not_retry_other_errors() {
        #[typedflake(epoch = "2025-01-01")]
        struct TestId(i64);

        let clock = mock::freeze(EPOCH + 10);
        let generator = TestId::generator(0).unwrap();
        generator.generate_async().await.unwrap();

        clock.set(EPOCH + 5);
        assert!(matches!(
            generator.generate_async().await,
            Err(GenerateError::ClockRollback { behind_millis: 5 })
        ));
    }
}