typed_index_collections/slice/
boxed.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use core::iter::FromIterator;
use core::mem::transmute;

#[cfg(feature = "alloc")]
use alloc::{boxed::Box, vec};

#[cfg(any(feature = "serde-alloc", feature = "serde-std"))]
use serde::de::{Deserialize, Deserializer};

use crate::{TiSlice, TiVec};

impl<K, V> From<Box<TiSlice<K, V>>> for Box<[V]> {
    #[inline]
    fn from(slice: Box<TiSlice<K, V>>) -> Self {
        // SAFETY: `TiSlice<K, V>` is `repr(transparent)` over a `[V]` type.
        unsafe { transmute::<Box<TiSlice<K, V>>, Self>(slice) }
    }
}

impl<K, V> From<Box<[V]>> for Box<TiSlice<K, V>> {
    #[inline]
    fn from(slice: Box<[V]>) -> Self {
        // SAFETY: `TiSlice<K, V>` is `repr(transparent)` over a `[V]` type.
        unsafe { transmute::<Box<[V]>, Self>(slice) }
    }
}

impl<K, V: Clone> Clone for Box<TiSlice<K, V>> {
    #[inline]
    fn clone(&self) -> Self {
        self.to_vec().into_boxed_slice()
    }
}

impl<K, V> IntoIterator for Box<TiSlice<K, V>> {
    type Item = V;
    type IntoIter = vec::IntoIter<V>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.into_vec().into_iter()
    }
}

impl<K, V> Default for Box<TiSlice<K, V>> {
    #[inline]
    fn default() -> Self {
        TiVec::new().into()
    }
}

impl<K, V: Copy> From<&TiSlice<K, V>> for Box<TiSlice<K, V>> {
    #[inline]
    fn from(slice: &TiSlice<K, V>) -> Self {
        Box::<[V]>::from(&slice.raw).into()
    }
}

impl<K, V> From<Box<TiSlice<K, V>>> for TiVec<K, V> {
    #[inline]
    fn from(s: Box<TiSlice<K, V>>) -> Self {
        s.into_vec()
    }
}

impl<K, V> From<TiVec<K, V>> for Box<TiSlice<K, V>> {
    #[inline]
    fn from(v: TiVec<K, V>) -> Self {
        v.into_boxed_slice()
    }
}

impl<K, V> FromIterator<V> for Box<TiSlice<K, V>> {
    #[inline]
    fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
        iter.into_iter().collect::<TiVec<K, V>>().into_boxed_slice()
    }
}

#[cfg(any(feature = "serde-alloc", feature = "serde-std"))]
impl<'de, K, V: Deserialize<'de>> Deserialize<'de> for Box<TiSlice<K, V>> {
    #[inline]
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Box::<[V]>::deserialize(deserializer).map(Into::into)
    }
}

#[expect(dead_code, unused_imports, unused_mut, reason = "okay in tests")]
#[cfg(test)]
mod test {
    use core::borrow::{Borrow, BorrowMut};
    use core::hash::{Hash, Hasher};
    use core::ops::Bound;

    use alloc::borrow::{Cow, ToOwned};
    use alloc::boxed::Box;
    use alloc::ffi::CString;
    use alloc::string::ToString;
    use alloc::vec::Vec;

    #[cfg(feature = "std")]
    use std::hash::DefaultHasher;
    #[cfg(feature = "std")]
    use std::io::{IoSlice, Write};

    use crate::test_util::{AsSliceAndCapacity, Id};
    use crate::{TiSlice, TiVec};

    #[test]
    fn test_boxed_slice_api_compatibility() {
        for v in [
            &[0_u32; 0][..],
            &[1],
            &[1, 1234],
            &[1, 2, 4],
            &[1, 5, 3, 2],
            &[1, 1, 9, 2, 4, 1, 12345, 12],
        ] {
            let mut cv = (v, TiSlice::from_ref(v));
            assert_eq_api!(
                cv, v => Box::<TheSlice<u32>>::from(v) == <Box<TheSlice<u32>>>::default()
            );
            assert_eq_api!(cv, v => Box::<TheSlice<_>>::from(v).into_std());
            assert_eq_api!(cv, v => Box::<TheSlice<_>>::from(v).clone().into_std());
            assert_eq_api!(
                cv, v => IntoIterator::into_iter(Box::<TheSlice<u32>>::from(v)).collect::<Vec<_>>()
            );
            assert_eq_api!(cv, v => TheVec::from(Box::<TheSlice<_>>::from(v)).into_std());
            assert_eq_api!(cv, v => Box::<TheSlice<_>>::from(TheVec::from(v)).into_std());
            assert_eq_api!(cv, v => v.iter().copied().collect::<Box<TheSlice<_>>>().into_std());
        }
    }

    #[expect(clippy::unwrap_used, reason = "okay in tests")]
    #[cfg(feature = "serde")]
    #[test]
    fn test_boxed_slice_deserialize() {
        let s0: Box<TiSlice<Id, u32>> = serde_json::from_str("[]").unwrap();
        let s1: Box<TiSlice<Id, u32>> = serde_json::from_str("[12]").unwrap();
        let s2: Box<TiSlice<Id, u32>> = serde_json::from_str("[23, 34]").unwrap();
        assert_eq!(s0.as_ref().raw, [0; 0][..]);
        assert_eq!(s1.as_ref().raw, [12][..]);
        assert_eq!(s2.as_ref().raw, [23, 34][..]);
    }
}