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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*!
`brownstone` is a library for building fixed-size arrays. It provides a
collection of optimizer-friendly fallible and infallible function that build
arrays by calling initializer functions in a loop. It also provides a low-level
[builder type][builder::ArrayBuilder], with a [`push`][builder::ArrayBuilder::push`]
+ [`finish`][builder::ArrayBuilder::finish] interface, as well as a [misuse-
immune builder type][move_builder::ArrayBuilder] with a move-based interface
that can never panic or return errors.
*/

#![cfg_attr(not(feature = "std"), no_std)]

pub mod builder;
pub mod move_builder;

use core::{
    convert::Infallible,
    fmt::{self, Display, Formatter},
};

use move_builder::{ArrayBuilder, PushResult};

/// Error returned from the fallible `try_build_*` functions in this crate.
/// This includes the original error returned from the input function, along
/// with the index where the error occurred.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TryBuildError<E> {
    pub error: E,
    pub index: usize,
}

impl<E> Display for TryBuildError<E> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "error building array at index {}", self.index)
    }
}

#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> std::error::Error for TryBuildError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.error)
    }
}

#[inline]
const fn infallible<T>(value: T) -> Result<T, Infallible> {
    Ok(value)
}

/// Build a fixed-size array with a fallible initializer function. The
/// initializer is called once for each item in the length of the array, in
/// order; if it ever returns an `Err`, that error is propagated (along with
/// the index of the failed item).
///
/// Each time the method is called, it is provided with context in the form of
/// the prefix of the array that has already been initialized.
#[inline]
pub fn try_build_with<T, F, E, const N: usize>(
    mut next_value: F,
) -> Result<[T; N], TryBuildError<E>>
where
    F: for<'a> FnMut(&'a mut [T]) -> Result<T, E>,
{
    match ArrayBuilder::start() {
        PushResult::Full(array) => Ok(array),
        PushResult::NotFull(mut builder) => loop {
            let value =
                next_value(builder.finished_slice_mut()).map_err(|error| TryBuildError {
                    error,
                    index: builder.len(),
                })?;

            match builder.push(value) {
                PushResult::Full(array) => break Ok(array),
                PushResult::NotFull(updated) => builder = updated,
            }
        },
    }
}

/**
Build a fixed-size array with an initializer function. The initializer is
called once for each item in the length of the array, and the completed
array is returned.

Each time the method is called, it is provided with context in the form of
the prefix of the array that has already been initialized.

# Example

```
let fib: [i32; 10] = brownstone::build_with(|prefix| match *prefix {
    [] => 0,
    [_] => 1,
    [.., a, b] => a + b,
});

assert_eq!(fib, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
```
*/
#[inline]
pub fn build_with<T, F, const N: usize>(mut next_value: F) -> [T; N]
where
    F: for<'a> FnMut(&'a mut [T]) -> T,
{
    match try_build_with(move |slice| infallible(next_value(slice))) {
        Ok(array) => array,
        Err(inf) => match inf.error {},
    }
}

/**
Build a fixed-size array with a fallible initializer function. The
initializer is called once for each item in the length of the array, in
order; if it ever returns an `Err`, that error is propagated (along with
the index of the failed item).

Each time the method is called, it is provided with the index of the
element being produced.
*/
#[inline]
pub fn try_build_indexed<T, F, E, const N: usize>(
    mut next_value: F,
) -> Result<[T; N], TryBuildError<E>>
where
    F: FnMut(usize) -> Result<T, E>,
{
    try_build_with(move |slice| next_value(slice.len()))
}

/**
Build a fixed-size array with an initializer function. The initializer is
called once for each item in the length of the array, and the completed
array is returned.

Each time the method is called, it is provided with the index of the
element being produced.

# Example

```
let array: [usize; 5] = brownstone::build_indexed(|i| i + 1);
assert_eq!(array, [1, 2, 3, 4, 5]);
```

*/
#[inline]
pub fn build_indexed<T, F, const N: usize>(mut next_value: F) -> [T; N]
where
    F: FnMut(usize) -> T,
{
    build_with(move |slice| next_value(slice.len()))
}

/// Build a fixed-size array with a fallible initializer function. The
/// initializer is called once for each item in the length of the array, in
/// order; if it ever returns an `Err`, that error is propagated (along with
/// the index of the failed item).
#[inline]
pub fn try_build<T, F, E, const N: usize>(mut next_value: F) -> Result<[T; N], TryBuildError<E>>
where
    F: FnMut() -> Result<T, E>,
{
    try_build_with(move |_slice| next_value())
}

/**
Build a fixed-size array with an initializer function. The initializer is
called once for each item in the length of the array, and the completed
array is returned.

# Example

```
let array: [String; 5] = brownstone::build(|| format!("Hello"));
assert_eq!(array, ["Hello", "Hello", "Hello", "Hello", "Hello"]);
```
*/
#[inline]
pub fn build<T, F, const N: usize>(mut next_value: F) -> [T; N]
where
    F: FnMut() -> T,
{
    build_with(move |_slice| next_value())
}

/**
Build a fixed-size array from an iterator. The first `N` elements of the
iterator are collected into an array of length `N`. Returns `None` if the
iterator doesn't yield enough elements.

# Example

```
let array: [i32; 5] = brownstone::try_build_iter(1..).unwrap();
assert_eq!(array, [1, 2, 3, 4, 5]);
```

## Iterator too short

```
let array: Option<[i32; 10]> = brownstone::try_build_iter(1..5);
assert!(array.is_none());
```
*/
#[inline]
pub fn try_build_iter<I: IntoIterator, const N: usize>(iterator: I) -> Option<[I::Item; N]> {
    let mut iterator = iterator.into_iter();

    let (_min, max) = iterator.size_hint();

    // Preemptively check if the iterator will be too short.
    if let Some(max) = max {
        if max < N {
            return None;
        }
    }

    try_build(move || iterator.next().ok_or(())).ok()
}

/**
Build a fixed-size array from an iterator. The first `N` elements of the
iterator are collected into an array of length `N`.

# Panics

Panics if the iterator doesn't yield enough elements

# Example

```
let array: [i32; 5] = brownstone::build_iter(1..);
assert_eq!(array, [1, 2, 3, 4, 5]);
```
*/
#[inline]
pub fn build_iter<I: IntoIterator, const N: usize>(iterator: I) -> [I::Item; N] {
    try_build_iter(iterator).expect("build_iter: iterator too short")
}

/**
Build a fixed-size array out of clones of some element. The element itself
is used as the first element in the array.

# Example

```
let array: [Vec<i32>; 4] = brownstone::build_cloned(vec![1, 2, 3]);
assert_eq!(
    array,
    [
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
    ]
)
```
*/
#[inline]
pub fn build_cloned<T: Clone, const N: usize>(mut item: T) -> [T; N] {
    match ArrayBuilder::start() {
        PushResult::Full(array) => array,
        PushResult::NotFull(mut builder) => loop {
            builder = match builder.push(item) {
                PushResult::Full(array) => break array,
                PushResult::NotFull(builder) => builder,
            };
            item = builder.finished_slice()[0].clone();
        },
    }
}