Skip to main content

narrow/logical/
uuid.rs

1use uuid::Uuid;
2
3use crate::{
4    NonNullable, Nullable,
5    array::{ArrayType, FixedSizeBinary, UnionType},
6    buffer::BufferType,
7    offset::Offset,
8};
9
10use super::{LogicalArray, LogicalArrayType};
11
12impl ArrayType<uuid::Uuid> for uuid::Uuid {
13    type Array<Buffer: BufferType, OffsetItem: Offset, UnionLayout: UnionType> =
14        LogicalArray<Self, NonNullable, Buffer, OffsetItem, UnionLayout>;
15}
16
17impl ArrayType<uuid::Uuid> for Option<uuid::Uuid> {
18    type Array<Buffer: BufferType, OffsetItem: Offset, UnionLayout: UnionType> =
19        LogicalArray<uuid::Uuid, Nullable, Buffer, OffsetItem, UnionLayout>;
20}
21
22impl LogicalArrayType<uuid::Uuid> for uuid::Uuid {
23    type ArrayType = FixedSizeBinary<16>;
24
25    fn from_array_type(item: Self::ArrayType) -> Self {
26        Self::from_bytes(item.into())
27    }
28
29    fn into_array_type(self) -> Self::ArrayType {
30        Self::into_bytes(self).into()
31    }
32}
33
34#[cfg(feature = "arrow-rs")]
35impl crate::arrow::LogicalArrayType<uuid::Uuid> for uuid::Uuid {
36    type ExtensionType = arrow_schema::extension::Uuid;
37    fn extension_type() -> Option<Self::ExtensionType> {
38        Some(arrow_schema::extension::Uuid)
39    }
40}
41
42/// An array for [`Uuid`] items.
43#[allow(unused)]
44pub type UuidArray<Nullable = NonNullable, Buffer = crate::buffer::VecBuffer> =
45    LogicalArray<Uuid, Nullable, Buffer>;
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::Length;
51
52    #[test]
53    fn from_iter() {
54        let array = [Uuid::from_u128(1), Uuid::from_u128(42)]
55            .into_iter()
56            .collect::<UuidArray>();
57        assert_eq!(array.len(), 2);
58        assert_eq!(array.0.len(), 2);
59
60        let array_nullable = [Some(Uuid::from_u128(1)), None]
61            .into_iter()
62            .collect::<UuidArray<Nullable>>();
63        assert_eq!(array_nullable.len(), 2);
64        assert_eq!(array_nullable.0.len(), 2);
65    }
66
67    #[test]
68    fn into_iter() {
69        let input = [Uuid::from_u128(1), Uuid::from_u128(42)];
70        let array = input.into_iter().collect::<UuidArray>();
71        let output = array.into_iter().collect::<Vec<_>>();
72        assert_eq!(input, output.as_slice());
73
74        let input_nullable = [Some(Uuid::from_u128(1)), None];
75        let array_nullable = input_nullable.into_iter().collect::<UuidArray<Nullable>>();
76        let output_nullable = array_nullable.into_iter().collect::<Vec<_>>();
77        assert_eq!(input_nullable, output_nullable.as_slice());
78    }
79}