ext4_view/uuid.rs
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use core::fmt::{self, Debug, Display, Formatter};
10
11/// 128-bit UUID.
12///
13/// # Example
14///
15/// ```
16/// use ext4_view::Uuid;
17///
18/// let uuid = Uuid::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
19/// assert_eq!(format!("{uuid}"), "01020304-0506-0708-090a-0b0c0d0e0f10");
20/// ```
21#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
22pub struct Uuid(pub(crate) [u8; 16]);
23
24impl Uuid {
25 /// Create a UUID from raw bytes.
26 #[must_use]
27 pub const fn new(bytes: [u8; 16]) -> Self {
28 Self(bytes)
29 }
30
31 /// Get the raw bytes of the UUID.
32 #[must_use]
33 pub const fn as_bytes(&self) -> &[u8; 16] {
34 &self.0
35 }
36}
37
38impl Debug for Uuid {
39 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
40 write!(
41 f,
42 "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
43 self.0[0],
44 self.0[1],
45 self.0[2],
46 self.0[3],
47 self.0[4],
48 self.0[5],
49 self.0[6],
50 self.0[7],
51 self.0[8],
52 self.0[9],
53 self.0[10],
54 self.0[11],
55 self.0[12],
56 self.0[13],
57 self.0[14],
58 self.0[15]
59 )
60 }
61}
62
63impl Display for Uuid {
64 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
65 <Self as Debug>::fmt(self, f)
66 }
67}