Skip to main content

xtask_todo_lib/
id.rs

1//! Unique identifier for a todo item.
2
3use std::fmt;
4
5/// Unique identifier for a todo item. Opaque; use for completion and deletion.
6#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub struct TodoId(std::num::NonZeroU64);
8
9impl TodoId {
10    /// Creates a `TodoId` from a raw u64 (e.g. when loading from storage). Returns `None` if n is 0.
11    #[must_use]
12    pub fn from_raw(n: u64) -> Option<Self> {
13        std::num::NonZeroU64::new(n).map(TodoId)
14    }
15
16    /// Returns the raw numeric id (e.g. for serialization).
17    #[must_use]
18    pub const fn as_u64(self) -> u64 {
19        self.0.get()
20    }
21}
22
23impl fmt::Display for TodoId {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        self.0.fmt(f)
26    }
27}