Skip to content

Commit b518379

Browse files
authored
chore: cleanup (#111)
1 parent 9908c37 commit b518379

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

src/lib.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl UUID {
8787
}
8888

8989
fn __repr__(&self) -> String {
90-
format!("UUID('{}')", self.__str__())
90+
format!("UUID('{}')", self.uuid.hyphenated())
9191
}
9292

9393
fn __richcmp__(&self, other: UUID, op: CompareOp) -> PyResult<bool> {
@@ -135,8 +135,8 @@ impl UUID {
135135
}
136136

137137
#[getter]
138-
fn hex(&self) -> PyResult<String> {
139-
Ok(self.uuid.simple().to_string())
138+
fn hex(&self) -> String {
139+
self.uuid.simple().to_string()
140140
}
141141

142142
#[getter]
@@ -146,12 +146,7 @@ impl UUID {
146146

147147
#[getter]
148148
fn bytes_le<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
149-
let b = self.uuid.as_bytes();
150-
let bytes: [u8; 16] = [
151-
b[3], b[2], b[1], b[0], b[5], b[4], b[7], b[6], b[8], b[9], b[10], b[11], b[12], b[13],
152-
b[14], b[15],
153-
];
154-
PyBytes::new(py, &bytes)
149+
PyBytes::new(py, &self.uuid.to_bytes_le())
155150
}
156151

157152
#[getter]
@@ -160,8 +155,8 @@ impl UUID {
160155
}
161156

162157
#[getter]
163-
fn urn(&self) -> PyResult<String> {
164-
Ok(self.uuid.urn().to_string())
158+
fn urn(&self) -> String {
159+
self.uuid.urn().to_string()
165160
}
166161

167162
#[getter]
@@ -237,15 +232,16 @@ impl UUID {
237232
}
238233

239234
#[getter]
240-
fn fields(&self) -> PyResult<(u32, u16, u16, u8, u8, u64)> {
241-
Ok((
242-
self.time_low(),
243-
self.time_mid(),
244-
self.time_hi_version(),
245-
self.clock_seq_hi_variant(),
246-
self.clock_seq_low(),
247-
self.node(),
248-
))
235+
fn fields(&self) -> (u32, u16, u16, u8, u8, u64) {
236+
let int = self.uuid.as_u128();
237+
(
238+
int.wrapping_shr(96) as u32, // time_low
239+
((int.wrapping_shr(80)) & 0xffff) as u16, // time_mid
240+
((int.wrapping_shr(64)) & 0xffff) as u16, // time_hi_version
241+
((int.wrapping_shr(56)) & 0xff) as u8, // clock_seq_hi_variant
242+
((int.wrapping_shr(48)) & 0xff) as u8, // clock_seq_low
243+
(int & 0xffffffffffff) as u64, // node
244+
)
249245
}
250246

251247
#[staticmethod]
@@ -415,14 +411,9 @@ fn _getnode() -> u64 {
415411
bytes
416412
}
417413
};
418-
419-
let node = ((bytes[0] as u64) << 40)
420-
| ((bytes[1] as u64) << 32)
421-
| ((bytes[2] as u64) << 24)
422-
| ((bytes[3] as u64) << 16)
423-
| ((bytes[4] as u64) << 8)
424-
| (bytes[5] as u64);
425-
414+
let node = u64::from_be_bytes([
415+
0, 0, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5],
416+
]);
426417
NODE.store(node, Ordering::Relaxed);
427418
node
428419
}

0 commit comments

Comments
 (0)