repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

refactor: align crates with dependency law

7e70ce86bff0177e34b3421350dc10d7392aaee9

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

+22 / −27 text lines

Diff layout

smith-core/src/session.rs

Modified

+1 / −1 text lines

405405 branch_id: if is_active {
406406 session.active_branch().branch_id
407407 } else {
408 BranchId::from(leaf.id.as_uuid())
408 BranchId::from_u128(leaf.id.as_u128())
409409 },
410410 entries,
411411 selected_idx,

smith/Cargo.toml

Modified

+1 / −4 text lines

2121[[bench]]
2222name = "message_creation"
23harness = false
25[features]
26default = []No final newline
23harness = falseNo final newline

smith/src/id.rs

Modified

+17 / −15 text lines

2424 Self(Uuid::now_v7())
2525 }
27 /// The underlying UUID.
27 /// The identifier bits.
2828 #[must_use]
29 pub const fn as_uuid(&self) -> Uuid {
30 self.0
29 pub const fn as_u128(&self) -> u128 {
30 self.0.as_u128()
31 }
33 /// Rebuild an identifier from its bits.
34 #[must_use]
35 pub const fn from_u128(bits: u128) -> Self {
36 Self(Uuid::from_u128(bits))
37 }
39 /// Unix milliseconds encoded in the `UUIDv7` timestamp, if any.
40 #[must_use]
41 pub fn unix_millis(&self) -> Option<u64> {
42 let timestamp = self.0.get_timestamp()?;
43 let (seconds, nanos) = timestamp.to_unix();
44 Some(seconds * 1_000 + u64::from(nanos) / 1_000_000)
3145 }
3246 }
3751 }
3852 }
40 impl From<Uuid> for $name {
41 fn from(value: Uuid) -> Self {
42 Self(value)
43 }
44 }
46 impl From<$name> for Uuid {
47 fn from(id: $name) -> Self {
48 id.0
49 }
50 }
5254 impl Default for $name {
5355 fn default() -> Self {
5456 Self::new()

smith/src/lib.rs

Modified

+1 / −1 text lines

4040 let a = SessionId::new();
4141 let b = SessionId::new();
4242 assert_ne!(a, b);
43 assert_eq!(a, SessionId::from(a.as_uuid()));
43 assert_eq!(a, SessionId::from_u128(a.as_u128()));
4444 }
4646 #[test]

smith/src/message.rs

Modified

+2 / −6 text lines

169169/// This keeps identity and timestamp consistent and avoids a second clock read
170170/// on every message construction.
171171fn id_timestamp_ms(id: MessageId) -> i64 {
172 let Some(timestamp) = id.as_uuid().get_timestamp() else {
173 return 0;
174 };
175 let (seconds, nanos) = timestamp.to_unix();
176 let millis = u128::from(seconds) * 1_000 + u128::from(nanos / 1_000_000);
177 i64::try_from(millis).unwrap_or(i64::MAX)
172 id.unix_millis()
173 .map_or(0, |ms| i64::try_from(ms).unwrap_or(i64::MAX))