|
2 | 2 |
|
3 | 3 | use crate::datetime::HYPHEN; |
4 | 4 | use crate::typeref::*; |
| 5 | +use serde::ser::{Serialize, Serializer}; |
5 | 6 | use smallvec::SmallVec; |
6 | 7 | use std::io::Write; |
7 | 8 | use std::os::raw::c_uchar; |
8 | 9 |
|
9 | | -pub type UUIDBuffer = heapless::Vec<u8, heapless::consts::U64>; |
| 10 | +type UUIDBuffer = heapless::Vec<u8, heapless::consts::U64>; |
10 | 11 |
|
11 | | -#[inline(never)] |
12 | | -pub fn write_uuid(ptr: *mut pyo3::ffi::PyObject, buf: &mut UUIDBuffer) { |
13 | | - let value: u128; |
14 | | - { |
15 | | - // test_uuid_immutable, test_uuid_int |
16 | | - let py_int = ffi!(PyObject_GetAttr(ptr, INT_ATTR_STR)); |
17 | | - ffi!(Py_DECREF(py_int)); |
18 | | - let buffer: [c_uchar; 16] = [0; 16]; |
19 | | - unsafe { |
20 | | - // test_uuid_overflow |
21 | | - pyo3::ffi::_PyLong_AsByteArray( |
22 | | - py_int as *mut pyo3::ffi::PyLongObject, |
23 | | - buffer.as_ptr() as *const c_uchar, |
24 | | - 16, |
25 | | - 1, // little_endian |
26 | | - 0, // is_signed |
27 | | - ) |
28 | | - }; |
29 | | - value = u128::from_le_bytes(buffer); |
| 12 | +pub struct UUID { |
| 13 | + ptr: *mut pyo3::ffi::PyObject, |
| 14 | +} |
| 15 | + |
| 16 | +impl UUID { |
| 17 | + pub fn new(ptr: *mut pyo3::ffi::PyObject) -> Self { |
| 18 | + UUID { ptr: ptr } |
30 | 19 | } |
| 20 | +} |
| 21 | +impl<'p> Serialize for UUID { |
| 22 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 23 | + where |
| 24 | + S: Serializer, |
| 25 | + { |
| 26 | + let value: u128; |
| 27 | + { |
| 28 | + // test_uuid_immutable, test_uuid_int |
| 29 | + let py_int = ffi!(PyObject_GetAttr(self.ptr, INT_ATTR_STR)); |
| 30 | + ffi!(Py_DECREF(py_int)); |
| 31 | + let buffer: [c_uchar; 16] = [0; 16]; |
| 32 | + unsafe { |
| 33 | + // test_uuid_overflow |
| 34 | + pyo3::ffi::_PyLong_AsByteArray( |
| 35 | + py_int as *mut pyo3::ffi::PyLongObject, |
| 36 | + buffer.as_ptr() as *const c_uchar, |
| 37 | + 16, |
| 38 | + 1, // little_endian |
| 39 | + 0, // is_signed |
| 40 | + ) |
| 41 | + }; |
| 42 | + value = u128::from_le_bytes(buffer); |
| 43 | + } |
31 | 44 |
|
32 | | - let mut hexadecimal: SmallVec<[u8; 32]> = SmallVec::with_capacity(32); |
33 | | - write!(hexadecimal, "{:032x}", value).unwrap(); |
| 45 | + let mut hexadecimal: SmallVec<[u8; 32]> = SmallVec::with_capacity(32); |
| 46 | + write!(hexadecimal, "{:032x}", value).unwrap(); |
34 | 47 |
|
35 | | - buf.extend_from_slice(&hexadecimal[..8]).unwrap(); |
36 | | - buf.push(HYPHEN).unwrap(); |
37 | | - buf.extend_from_slice(&hexadecimal[8..12]).unwrap(); |
38 | | - buf.push(HYPHEN).unwrap(); |
39 | | - buf.extend_from_slice(&hexadecimal[12..16]).unwrap(); |
40 | | - buf.push(HYPHEN).unwrap(); |
41 | | - buf.extend_from_slice(&hexadecimal[16..20]).unwrap(); |
42 | | - buf.push(HYPHEN).unwrap(); |
43 | | - buf.extend_from_slice(&hexadecimal[20..]).unwrap(); |
| 48 | + let mut buf: UUIDBuffer = heapless::Vec::new(); |
| 49 | + buf.extend_from_slice(&hexadecimal[..8]).unwrap(); |
| 50 | + buf.push(HYPHEN).unwrap(); |
| 51 | + buf.extend_from_slice(&hexadecimal[8..12]).unwrap(); |
| 52 | + buf.push(HYPHEN).unwrap(); |
| 53 | + buf.extend_from_slice(&hexadecimal[12..16]).unwrap(); |
| 54 | + buf.push(HYPHEN).unwrap(); |
| 55 | + buf.extend_from_slice(&hexadecimal[16..20]).unwrap(); |
| 56 | + buf.push(HYPHEN).unwrap(); |
| 57 | + buf.extend_from_slice(&hexadecimal[20..]).unwrap(); |
| 58 | + serializer.serialize_str(str_from_slice!(buf.as_ptr(), buf.len())) |
| 59 | + } |
44 | 60 | } |
0 commit comments