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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::ops::{Deref, DerefMut};
use std::slice;

/// A contiguous growable array type that contains one or more items (never zero).
///
/// This vector type is similar to [`Vec`], but is **never** empty and guarantees that it contains
/// at least one item.
///
/// [`Vec`]: std::vec::Vec
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Vec1<T> {
    items: Vec<T>,
}

impl<T> Vec1<T> {
    /// Constructs a `Vec1` from an item.
    pub fn from_item(item: T) -> Self {
        Vec1 { items: vec![item] }
    }

    /// Constructs a `Vec1` from a head item and zero or more tail items.
    pub fn from_head_and_tail(head: T, tail: impl IntoIterator<Item = T>) -> Self {
        Vec1 { items: Some(head).into_iter().chain(tail).collect() }
    }

    /// Constructs a `Vec1` from an [`IntoIterator`] of items.
    ///
    /// # Errors
    ///
    /// Returns an error if the [`IntoIterator`] is empty.
    ///
    /// [`IntoIterator`]: std::iter::IntoIterator
    pub fn try_from_iter(items: impl IntoIterator<Item = T>) -> Result<Self, ()> {
        let mut items = items.into_iter();
        match items.next() {
            Some(head) => Ok(Vec1::from_head_and_tail(head, items)),
            _ => Err(()),
        }
    }

    // TODO(https://fxbug.dev/352335343): These iterator-like operations should instead be
    // implemented by an `Iterator1` (and arguably `Slice1`) type.
    /// Maps the items from the vector into another.
    pub fn map_into<U, F>(self, f: F) -> Vec1<U>
    where
        F: FnMut(T) -> U,
    {
        Vec1 { items: self.items.into_iter().map(f).collect() }
    }

    /// Fallibly maps the items in the vector by reference to either another vector or an error.
    ///
    /// # Errors
    ///
    /// Returns an error if the mapping function returns an error for some item in the vector.
    pub fn try_map_ref<U, E, F>(&self, f: F) -> Result<Vec1<U>, E>
    where
        F: FnMut(&T) -> Result<U, E>,
    {
        self.items.iter().map(f).collect::<Result<Vec<_>, _>>().map(|items| Vec1 { items })
    }

    /// Fallibly maps the items in the vector by mutable reference to either another vector or an
    /// error.
    ///
    /// # Errors
    ///
    /// Returns an error if the mapping function returns an error for some item in the vector.
    pub fn try_map_mut<U, E, F>(&mut self, f: F) -> Result<Vec1<U>, E>
    where
        F: FnMut(&mut T) -> Result<U, E>,
    {
        self.items.iter_mut().map(f).collect::<Result<Vec<_>, _>>().map(|items| Vec1 { items })
    }

    /// Gets an iterator over the items in the vector.
    pub fn iter(&self) -> slice::Iter<'_, T> {
        self.items.iter()
    }

    /// Gets an iterator over the items in the vector.
    pub fn iter_mut(&mut self) -> slice::IterMut<'_, T> {
        self.items.iter_mut()
    }

    /// Gets a slice over the items in the vector.
    pub fn as_slice(&self) -> &[T] {
        self.items.as_slice()
    }

    /// Gets a slice over the items in the vector.
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        self.items.as_mut_slice()
    }
}

impl<T> AsMut<[T]> for Vec1<T> {
    fn as_mut(&mut self) -> &mut [T] {
        self.items.as_mut()
    }
}

impl<T> AsRef<[T]> for Vec1<T> {
    fn as_ref(&self) -> &[T] {
        self.items.as_ref()
    }
}

impl<T> Deref for Vec1<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.items.deref()
    }
}

impl<T> DerefMut for Vec1<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.items.deref_mut()
    }
}

impl<T> From<Vec1<T>> for Vec<T> {
    fn from(one_or_more: Vec1<T>) -> Self {
        one_or_more.items
    }
}

impl<T> IntoIterator for Vec1<T> {
    type Item = T;
    type IntoIter = std::vec::IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        self.items.into_iter()
    }
}

impl<T> TryFrom<Vec<T>> for Vec1<T> {
    type Error = ();

    fn try_from(items: Vec<T>) -> Result<Self, Self::Error> {
        if items.is_empty() {
            Err(())
        } else {
            Ok(Vec1 { items })
        }
    }
}

macro_rules! with_literals {
    ($f:ident$(,)?) => {};
    ($f:ident, [ $($N:literal),+ $(,)? ]$(,)?) => {
        $(
            $f!($N);
        )+
    };
}
macro_rules! impl_from_array_for_vec1 {
    ($N:literal $(,)?) => {
        impl<T> From<[T; $N]> for Vec1<T> {
            fn from(items: [T; $N]) -> Self {
                Vec1 { items: items.into() }
            }
        }
    };
}
with_literals!(impl_from_array_for_vec1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);

#[cfg(test)]
mod tests {
    use crate::experimental::vec1::Vec1;

    #[test]
    fn vec1_from_item_or_head_and_tail() {
        let xs = Vec1::from_item(0i32);
        assert_eq!(xs.as_slice(), &[0]);

        let xs = Vec1::from_head_and_tail(0i32, []);
        assert_eq!(xs.as_slice(), &[0]);

        let xs = Vec1::from_head_and_tail(0i32, [1, 2]);
        assert_eq!(xs.as_slice(), &[0, 1, 2]);
    }

    #[test]
    fn vec1_ref() {
        let mut xs = Vec1::from_item(0i32);
        assert_eq!(xs.as_slice(), &[0]);
        assert_eq!(xs.as_ref(), &[0]);
        assert_eq!(xs.as_mut_slice(), &mut [0]);
        assert_eq!(xs.as_mut(), &mut [0]);
    }

    #[test]
    fn vec1_from_array() {
        let xs = Vec1::from([0i32]);
        assert_eq!(xs.as_slice(), &[0]);

        let xs = Vec1::from([0i32, 1, 2]);
        assert_eq!(xs.as_slice(), &[0, 1, 2]);
    }

    #[test]
    fn vec1_try_from_vec() {
        let xs = Vec1::<i32>::try_from(vec![]);
        assert!(xs.is_err());

        let xs = Vec1::try_from(vec![0i32]).unwrap();
        assert_eq!(xs.as_slice(), &[0]);

        let xs = Vec1::try_from(vec![0i32, 1, 2]).unwrap();
        assert_eq!(xs.as_slice(), &[0, 1, 2]);
    }

    #[test]
    fn vec1_try_from_iter() {
        let xs = Vec1::<i32>::try_from_iter([]);
        assert!(xs.is_err());

        let xs = Vec1::try_from_iter([0i32]).unwrap();
        assert_eq!(xs.as_slice(), &[0]);

        let xs = Vec1::try_from_iter([0i32, 1, 2]).unwrap();
        assert_eq!(xs.as_slice(), &[0, 1, 2]);
    }

    #[test]
    fn vec1_iter() {
        let xs = Vec1::from([0i32, 1, 2]);
        let ys: Vec<_> = xs.iter().map(|x| x + 1).collect();
        assert_eq!(ys.as_slice(), &[1, 2, 3]);
    }

    #[test]
    fn vec1_map_into() {
        let xs = Vec1::from([0i32, 1, 2]);
        let ys = xs.map_into(|x| x + 1);
        assert_eq!(ys.as_slice(), &[1, 2, 3]);
    }

    #[test]
    fn vec1_try_map_ref() {
        let xs = Vec1::from([0i32, 1, 2]);
        let ys: Result<_, ()> = xs.try_map_ref(|x| Ok(*x + 1));
        assert_eq!(ys.unwrap().as_slice(), &[1, 2, 3]);
    }

    #[test]
    fn vec1_try_map_mut() {
        let mut xs = Vec1::from([0i32, 1, 2]);
        let ys: Result<_, ()> = xs.try_map_mut(|x| Ok(*x + 1));
        assert_eq!(ys.unwrap().as_slice(), &[1, 2, 3]);
    }
}