allo_isolate/uuid.rs
1//! uuid type
2
3use crate::{
4 ffi::{DartCObject, DartHandleFinalizer, DartTypedDataType},
5 into_dart::{free_zero_copy_buffer_u8, DartTypedDataTypeTrait},
6 IntoDart,
7};
8
9impl IntoDart for uuid::Uuid {
10 /// delegate to `Vec<u8>` implementation
11 ///
12 /// on the other side of FFI, value should be reconstructed like:
13 ///
14 /// - hydrate into Dart [UuidValue](https://pub.dev/documentation/uuid/latest/uuid/UuidValue-class.html)
15 /// `UuidValue.fromByteList(raw);`
16 ///
17 /// - hydrate into Rust [Uuid](uuid::Uuid)
18 /// ```rust,ignore
19 /// uuid::Uuid::from_bytes(*<&[u8] as std::convert::TryInto<&[u8;16]>>::try_into(raw.as_slice()).expect("invalid uuid slice"));
20 /// ```
21 fn into_dart(self) -> DartCObject {
22 self.as_bytes().to_vec().into_dart()
23 }
24}
25
26impl IntoDart for Vec<uuid::Uuid> {
27 /// ⚠️ concatenated in a single `Vec<u8>` for performance optimization
28 ///
29 /// on the other side of FFI, value should be reconstructed like:
30 ///
31 /// - hydrate into Dart List<[UuidValue](https://pub.dev/documentation/uuid/latest/uuid/UuidValue-class.html)>
32 /// ```dart
33 /// return List<UuidValue>.generate(
34 /// raw.lengthInBytes / 16,
35 /// (int i) => UuidValue.fromByteList(Uint8List.view(raw.buffer, i * 16, 16)),
36 /// growable: false);
37 /// ```
38 ///
39 /// - hydrate into Rust Vec<[Uuid](uuid::Uuid)>
40 /// ```rust,ignore
41 /// raw
42 /// .as_slice()
43 /// .chunks(16)
44 /// .map(|x: &[u8]| uuid::Uuid::from_bytes(*<&[u8] as std::convert::TryInto<&[u8;16]>>::try_into(x).expect("invalid uuid slice")))
45 /// .collect();
46 /// ```
47 ///
48 /// note that buffer could end up being incomplete under the same conditions as of [std::io::Write::write](https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.write).
49 fn into_dart(self) -> DartCObject {
50 use std::io::Write;
51 let mut buffer = Vec::<u8>::with_capacity(self.len() * 16);
52 for id in self {
53 let _ = buffer.write(id.as_bytes());
54 }
55 buffer.into_dart()
56 }
57}
58
59impl<const N: usize> IntoDart for [uuid::Uuid; N] {
60 fn into_dart(self) -> DartCObject {
61 let vec: Vec<_> = self.into();
62 vec.into_dart()
63 }
64}
65
66impl DartTypedDataTypeTrait for uuid::Uuid {
67 fn dart_typed_data_type() -> DartTypedDataType {
68 DartTypedDataType::Uint8
69 }
70
71 fn function_pointer_of_free_zero_copy_buffer() -> DartHandleFinalizer {
72 free_zero_copy_buffer_u8
73 }
74}