clt_database/alloc/collections/
boxed.rs1use super::{TryClone, TursoBoxExt, TursoFromIterator, TursoNewExt, TursoTryNewExt};
2use crate::alloc::{AllocError, Box, TryReserveError};
3
4fn boxed<T>(value: T) -> Box<T> {
5 Box::new(value)
6}
7
8fn try_boxed<T>(value: T) -> Result<Box<T>, AllocError> {
9 Ok(Box::new(value))
10}
11
12fn collect_boxed_slice<T, I>(iter: I) -> Box<[T]>
13where
14 I: IntoIterator<Item = T>,
15{
16 iter.into_iter()
17 .collect::<std::vec::Vec<_>>()
18 .into_boxed_slice()
19}
20
21fn empty_boxed_slice<T>() -> Box<[T]> {
22 std::vec::Vec::new().into_boxed_slice()
23}
24
25impl<T> TursoNewExt<T> for Box<T> {
26 fn new(value: T) -> Self {
27 boxed(value)
28 }
29}
30
31impl<T> TursoTryNewExt<T> for Box<T> {
32 fn try_new(value: T) -> Result<Self, AllocError> {
33 try_boxed(value)
34 }
35}
36
37impl<T> TursoBoxExt<T> for Box<T> {
38 fn into_inner(self) -> T {
39 *self
40 }
41}
42
43impl<T: Clone> TryClone for Box<T> {
44 type Error = AllocError;
45
46 fn try_clone(&self) -> Result<Self, Self::Error> {
47 <Self as TursoTryNewExt<T>>::try_new((**self).clone())
48 }
49}
50
51impl<T> TursoFromIterator<T> for Box<[T]> {
52 #[inline(always)]
53 fn try_from_iter<I>(iter: I) -> Result<Self, TryReserveError>
54 where
55 I: IntoIterator<Item = T>,
56 {
57 Ok(collect_boxed_slice(iter))
58 }
59
60 #[inline(always)]
61 fn try_extend<I>(&mut self, iter: I) -> Result<(), TryReserveError>
62 where
63 I: IntoIterator<Item = T>,
64 {
65 let mut values = std::mem::replace(self, empty_boxed_slice()).into_vec();
66 values.extend(iter);
67 *self = values.into_boxed_slice();
68 Ok(())
69 }
70}