| 250 | #[must_use] |
| 251 | #[expect(clippy::big_endian_bytes, reason = "We want to be deterministic")] |
| 252 | pub fn encode(self) -> Uuid { |
| 253 | // UUID v8 (128 bits, 16 bytes) |
| 254 | // Bytes (0-based, BE layout): |
| 255 | // +-------+-------+-------+-------+-------+-------+-------+-------+ |
| 256 | // | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
| 257 | // | run ID (u16) | stage (u16) | shard ID | V | L | retry | |
| 258 | // | | | | e | o | | |
| 259 | // | | | | r | 4 | | |
| 260 | // +-------+-------+-------+-------+-------+-------+-------+-------+ |
| 261 | // | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | |
| 262 | // | V | L | scope | sub_scope | local ID | |
| 263 | // | a | o | | | | |
| 264 | // | r | 4 | | | | |
| 265 | // +-------+-------+-------+-------+-------+-------+-------+-------+ |
| 266 | // Legend: |
| 267 | // - Bytes 0-1: run_id(u16, BE) |
| 268 | // - Bytes 2-3: stage_id(u16, BE) |
| 269 | // - Bytes 4-5: shard_id(u16, BE) |
| 270 | // - Byte 6: bits 7..4: Version (lib); bits 3..0: producer[31..28] |
| 271 | // - Byte 7: retry |
| 272 | // - Byte 8: bits 7..6: Variant (lib); bits 5..4: provenance; bits 3..0: |
| 273 | // producer[27..24] |
| 274 | // - Bytes 9: scope |
| 275 | // - Bytes 10-11: sub_scope |
| 276 | // - Bytes 12-15: local_id(u32, BE) |
| 277 | |
| 278 | let mut bytes = [0; 16]; |
| 279 | |
| 280 | // Bytes 0–1: run_id (u16) |
| 281 | bytes[0..2].copy_from_slice(&self.run_id.0.to_be_bytes()); |
| 282 | |
| 283 | // Bytes 2–3: stage_id (u16) |
| 284 | bytes[2..4].copy_from_slice(&self.stage_id.0.to_be_bytes()); |
| 285 | |
| 286 | // Bytes 4–5: shard_id (u16) |
| 287 | bytes[4..6].copy_from_slice(&self.shard_id.0.to_be_bytes()); |
| 288 | |
| 289 | // Byte 6: high nibble = Version (uuid crate overrides), low nibble = producer hi4 |
| 290 | bytes[6] |= self.producer as u8 >> 4; |
| 291 | |
| 292 | // Byte 7: retry |
| 293 | bytes[7] = self.retry; |
| 294 | |
| 295 | // Byte 8: |
| 296 | // high 2 bits = Variant (uuid crate overrides) |
| 297 | // bits 5..4 = provenance (2 Bit) |
| 298 | // low nibble = producer lo4 |
| 299 | bytes[8] = (((self.provenance as u8) & 0b11) << 4) | (self.producer as u8 & 0x0F); |
| 300 | |
| 301 | // Byte 9: scope |
| 302 | bytes[9] = self.scope as u8; |
| 303 | |
| 304 | // Bytes 10–11: sub_scope (u16) |
| 305 | bytes[10..12].copy_from_slice(&(self.sub_scope as u16).to_be_bytes()); |
| 306 | |
| 307 | // Bytes 12–15: local_id (u32) |
| 308 | bytes[12..16].copy_from_slice(&self.local_id.0.to_be_bytes()); |
| 309 | |