slice_utils/lib.rs
1#![cfg_attr(not(any(test, feature = "std")), no_std)]
2#![doc = include_str!("../README.md")]
3#![warn(missing_docs)]
4
5mod chain;
6mod chunks;
7mod cycle;
8mod debug;
9mod eq;
10mod fromfn;
11mod impls;
12mod index;
13mod interleave;
14mod iter;
15mod map;
16mod reverse;
17mod slicing;
18mod windows;
19mod zip;
20
21#[cfg(test)]
22mod test;
23
24use core::ops::RangeBounds;
25
26pub use chain::Chain;
27pub use chunks::{ArrayChunksBorrowed, ArrayChunksOwned, ChunksBorrowed, ChunksOwned};
28pub use cycle::Cycle;
29pub use fromfn::FromFn;
30pub use interleave::Interleave;
31pub use iter::{IterBorrowed, IterOwned};
32pub use map::{MapBorrowed, MapOwned};
33pub use reverse::Reverse;
34pub use slicing::{SliceOf, SplitMut};
35pub use windows::{ArrayWindowsBorrowed, ArrayWindowsOwned, WindowsBorrowed, WindowsOwned};
36pub use zip::Zip;
37
38/// Clones each item on access; see [`SliceBorrowed::cloned`].
39pub type Cloned<S> = MapBorrowed<S, for<'a> fn(&<S as Slice>::Output) -> <S as Slice>::Output>;
40
41/// The base trait for [`SliceOwned`], [`SliceBorrowed`], and [`SliceMut`].
42pub trait Slice {
43 /// The type this slice returns; analagous to
44 /// [`Index::Output`](core::ops::Index::Output).
45 type Output;
46
47 /// Returns the length of the slice.
48 fn len(&self) -> usize;
49
50 /// Returns whether or not the slice is empty.
51 ///
52 /// Equivalent to `slice.len() == 0`.
53 fn is_empty(&self) -> bool {
54 self.len() == 0
55 }
56
57 /// Call a closure with the indicated element, returning the result or
58 /// `None` if the index was out-of-bounds.
59 ///
60 /// This allows operations that are independent of indexing method.
61 ///
62 /// # Examples
63 ///
64 /// ```rust
65 /// # use slice_utils::Slice;
66 /// let a = [1_i32, 2, 3];
67 ///
68 /// // Doesn't care about ownership
69 /// fn stringify_first<S>(slice: &S) -> String
70 /// where
71 /// S: Slice,
72 /// S::Output: ToString,
73 /// {
74 /// slice.get_with(0, &mut |x| x.to_string()).unwrap_or_default()
75 /// }
76 ///
77 /// assert_eq!(stringify_first(&a), "1");
78 /// ```
79 fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R>;
80
81 /// Chains two slices together, back-to-back.
82 ///
83 /// Analagous to [`Iterator::chain`].
84 ///
85 /// # Examples
86 ///
87 /// ```rust
88 /// # use slice_utils::Slice;
89 /// let a = [1, 2, 3];
90 /// let b = [4, 5, 6];
91 ///
92 /// assert_eq!(a.chain(b), [1, 2, 3, 4, 5, 6]);
93 /// ```
94 fn chain<S: Slice<Output = Self::Output>>(self, other: S) -> Chain<Self, S>
95 where
96 Self: Sized,
97 {
98 Chain(self, other)
99 }
100
101 /// Cycles the slice infinitely.
102 ///
103 /// Analagous to [`Iterator::cycle`].
104 ///
105 /// # Examples
106 ///
107 /// ```rust
108 /// # use slice_utils::{Slice, SliceOwned};
109 /// let slice = [1, 2, 3].cycle();
110 /// assert_eq!(slice.get_owned(2), Some(3));
111 /// assert_eq!(slice.get_owned(4), Some(2));
112 /// assert_eq!(slice.get_owned(6), Some(1));
113 /// ```
114 fn cycle(self) -> Cycle<Self>
115 where
116 Self: Sized,
117 {
118 Cycle(self)
119 }
120
121 /// Interleaves two slices, e.g. [A, B, A, B, ...].
122 ///
123 /// # Examples
124 ///
125 /// ```rust
126 /// # use slice_utils::Slice;
127 /// let a = [1, 2, 3];
128 /// let b = [4, 5, 6];
129 /// let c = a.interleave(b);
130 ///
131 /// assert_eq!(c, [1, 4, 2, 5, 3, 6]);
132 /// ```
133 fn interleave<S: Slice<Output = Self::Output>>(self, other: S) -> Interleave<Self, S>
134 where
135 Self: Sized,
136 {
137 Interleave(self, other)
138 }
139
140 /// Reverses the slice.
141 ///
142 /// Analagous to [`Iterator::rev`].
143 ///
144 /// # Examples
145 ///
146 /// ```rust
147 /// # use slice_utils::Slice;
148 /// let slice = [1, 2, 3].rev();
149 /// assert_eq!(slice, [3, 2, 1]);
150 /// ```
151 fn rev(self) -> Reverse<Self>
152 where
153 Self: Sized,
154 {
155 Reverse(self)
156 }
157
158 /// Create a sub-slice of the slice.
159 ///
160 /// Analagous to slicing `&[T]`.
161 ///
162 /// # Examples
163 ///
164 /// ```rust
165 /// # use slice_utils::Slice;
166 /// let slice = [1, 2, 3, 4, 5].slice(1..4).unwrap();
167 /// assert_eq!(slice, [2, 3, 4]);
168 /// ```
169 fn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<Self>>
170 where
171 Self: Sized,
172 {
173 SliceOf::new(self, range)
174 }
175
176 /// Returns `(&self[..at], &self[at..])`.
177 /// Returns `None` if `at` is out-of-bounds.
178 ///
179 /// Analagous to [`slice::split`].
180 ///
181 /// # Examples
182 ///
183 /// ```rust
184 /// # use slice_utils::Slice;
185 /// let slice = [1, 2, 3, 4, 5, 6];
186 /// let (a, b) = slice.split(3).unwrap();
187 ///
188 /// assert_eq!(a, [1, 2, 3]);
189 /// assert_eq!(b, [4, 5, 6]);
190 /// ```
191 fn split(&self, at: usize) -> Option<(SliceOf<&Self>, SliceOf<&Self>)> {
192 Some((SliceOf::new(self, ..at)?, SliceOf::new(self, at..)?))
193 }
194}
195
196/// A [`Slice`] that can return borrowed values.
197pub trait SliceBorrowed: Slice {
198 /// Index the slice, returning a borrowed value.
199 fn get(&self, index: usize) -> Option<&Self::Output>;
200
201 /// Return an iterator over arrays covering consecutive portions of the
202 /// slice.
203 ///
204 /// Analagous to [`slice::array_chunks`].
205 ///
206 /// # Panics
207 ///
208 /// If `N == 0`, panics.
209 ///
210 /// # Examples
211 ///
212 /// ```rust
213 /// # use slice_utils::SliceBorrowed;
214 /// let slice = [1, 2, 3, 4, 5];
215 /// let mut iter = slice.array_chunks::<2>();
216 ///
217 /// assert_eq!(iter.next(), Some([&1, &2]));
218 /// assert_eq!(iter.next(), Some([&3, &4]));
219 /// assert!(iter.next().is_none());
220 /// assert_eq!(iter.remainder(), [5]);
221 /// ```
222 fn array_chunks<const N: usize>(&self) -> ArrayChunksBorrowed<Self, N> {
223 ArrayChunksBorrowed::new(self)
224 }
225
226 /// Return a slice/iterator over arrays covering overlapping portions of the
227 /// slice.
228 ///
229 /// Analagous to [`slice::array_windows`].
230 ///
231 /// # Panics
232 ///
233 /// If `N == 0`, panics.
234 ///
235 /// # Examples
236 ///
237 /// ```rust
238 /// # use slice_utils::SliceBorrowed;
239 /// let slice = [1, 2, 3, 4, 5];
240 /// let mut windows = slice.array_windows::<3>();
241 ///
242 /// assert_eq!(windows.next(), Some([&1, &2, &3]));
243 /// assert_eq!(windows.next(), Some([&2, &3, &4]));
244 /// assert_eq!(windows.next(), Some([&3, &4, &5]));
245 /// assert!(windows.next().is_none());
246 /// # {
247 /// # use slice_utils::SliceOwned;
248 /// assert_eq!(windows.get_owned(1), Some([&2, &3, &4]));
249 /// # }
250 /// ```
251 fn array_windows<const N: usize>(&self) -> ArrayWindowsBorrowed<Self, N> {
252 ArrayWindowsBorrowed::new(self)
253 }
254
255 /// Return an iterator over slices covering consecutive portions of the
256 /// slice.
257 ///
258 /// Analagous to [`slice::chunks`].
259 ///
260 /// # Panics
261 ///
262 /// If `size == 0`, panics.
263 ///
264 /// # Examples
265 ///
266 /// ```rust
267 /// # use slice_utils::SliceBorrowed;
268 /// let slice = [1, 2, 3, 4, 5];
269 /// let mut iter = slice.chunks(2);
270 ///
271 /// assert_eq!(iter.next().unwrap(), [1, 2]);
272 /// assert_eq!(iter.next().unwrap(), [3, 4]);
273 /// assert_eq!(iter.next().unwrap(), [5]);
274 /// assert!(iter.next().is_none());
275 /// ```
276 fn chunks(&self, size: usize) -> ChunksBorrowed<Self> {
277 ChunksBorrowed::new(self, size)
278 }
279
280 /// Call a closure on index, returning a new type.
281 ///
282 /// Analagous to [`Iterator::map`].
283 ///
284 /// # Examples
285 ///
286 /// ```rust
287 /// # use slice_utils::SliceBorrowed;
288 /// let slice = [0, 1, 2].map(|x| x != 0);
289 /// assert_eq!(slice, [false, true, true]);
290 /// ```
291 fn map<F: Fn(&Self::Output) -> R, R>(self, f: F) -> MapBorrowed<Self, F>
292 where
293 Self: Sized,
294 {
295 MapBorrowed(self, f)
296 }
297
298 /// Create a new slice that clones each value on access.
299 /// Analagous to <code>self.[map](SliceBorrowed::map)([Clone::clone])</code>.
300 ///
301 /// # Examples
302 ///
303 /// ```rust
304 /// # use slice_utils::SliceBorrowed;
305 /// # #[derive(Debug, PartialEq)]
306 /// struct Foo(u32);
307 /// impl Clone for Foo {
308 /// fn clone(&self) -> Foo { Foo(self.0 + 1) }
309 /// }
310 ///
311 /// let slice = [Foo(1), Foo(2)];
312
313 /// assert_eq!(slice.cloned(), [Foo(2), Foo(3)]);
314 /// ```
315 fn cloned(self) -> Cloned<Self>
316 where
317 Self: Sized,
318 Self::Output: Clone,
319 {
320 MapBorrowed(self, Clone::clone)
321 }
322
323 /// Creates an iterator over the slice.
324 ///
325 /// # Examples
326 ///
327 /// ```rust
328 /// # use slice_utils::{Slice, SliceBorrowed};
329 /// let slice = [1, 2].chain([3]);
330 /// let mut iter = slice.iter();
331 /// assert_eq!(iter.next(), Some(&1));
332 /// assert_eq!(iter.next(), Some(&2));
333 /// assert_eq!(iter.next(), Some(&3));
334 /// assert!(iter.next().is_none());
335 /// ```
336 fn iter(&self) -> IterBorrowed<Self> {
337 IterBorrowed::new(self)
338 }
339
340 /// Return a slice/iterator over slices covering overlapping portions of the
341 /// slice.
342 ///
343 /// Analagous to [`slice::windows`].
344 ///
345 /// # Panics
346 ///
347 /// If `size == 0`, panics.
348 ///
349 /// # Examples
350 ///
351 /// ```rust
352 /// # use slice_utils::SliceBorrowed;
353 /// let slice = [1, 2, 3, 4, 5];
354 /// let mut windows = slice.windows(3);
355 ///
356 /// assert_eq!(windows.next().unwrap(), [1, 2, 3]);
357 /// assert_eq!(windows.next().unwrap(), [2, 3, 4]);
358 /// assert_eq!(windows.next().unwrap(), [3, 4, 5]);
359 /// assert!(windows.next().is_none());
360 /// # {
361 /// # use slice_utils::SliceOwned;
362 /// assert_eq!(windows.get_owned(1).unwrap(), [2, 3, 4]);
363 /// # }
364 /// ```
365 fn windows(&self, size: usize) -> WindowsBorrowed<Self> {
366 WindowsBorrowed::new(self, size)
367 }
368}
369
370/// A [`Slice`] that can return mutably borrowed values.
371pub trait SliceMut: Slice {
372 /// Index the slice, returning a mutably borrowed value.
373 fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output>;
374
375 /// Returns `(&mut self[..at], &mut self[at..])`.
376 /// Returns `None` if `at` is out-of-bounds.
377 ///
378 /// Analagous to [`slice::split_mut`].
379 ///
380 /// To avoid aliasing, requires <code>Self: [Unique]</code>.
381 ///
382 /// # Examples
383 ///
384 /// ```rust
385 /// # use slice_utils::SliceMut;
386 /// let mut slice = [1, 2, 3, 4, 5];
387 /// let (mut left, mut right) = SliceMut::split_mut(&mut slice, 2).unwrap();
388 ///
389 /// assert_eq!(left, [1, 2, 3]);
390 /// assert_eq!(right, [4, 5]);
391 ///
392 /// left[0] = 0;
393 /// right[0] = 0;
394 /// assert_eq!(slice, [0, 2, 3, 0, 5]);
395 /// ```
396 fn split_mut(&mut self, at: usize) -> Option<(SplitMut<Self>, SplitMut<Self>)>
397 where
398 Self: Unique,
399 {
400 SplitMut::new(self, at)
401 }
402
403 /// Copy all the items from `src` into `self`.
404 ///
405 /// Similar to [`slice::clone_from_slice`].
406 ///
407 /// # Panics
408 ///
409 /// The lengths must match. If you only want to copy a sub-slice, you can
410 /// slice each side down to the desired range.
411 ///
412 /// # Examples
413 ///
414 /// ```rust
415 /// # use slice_utils::{Slice, SliceMut};
416 /// let mut x = [1, 2, 3, 4, 5];
417 /// let y = [6, 7];
418 ///
419 /// (&mut x).slice(3..).unwrap().copy_from_slice(&y);
420 ///
421 /// assert_eq!(x, [1, 2, 3, 6, 7]);
422 /// ```
423 fn copy_from_slice<S>(&mut self, src: &S)
424 where
425 S: Slice<Output = Self::Output>,
426 S::Output: Clone,
427 {
428 if self.len() != src.len() {
429 panic!("source slice length ({}) does not match destination slice length ({})",
430 src.len(), self.len())
431 }
432
433 for i in 0..src.len() {
434 src.get_with(i, &mut |item| *self.get_mut(i).unwrap_or_else(|| {
435 panic!("destination slice is shorter than source slice");
436 }) = item.clone());
437 }
438 }
439}
440
441/// A [`Slice`] that can return owned values.
442pub trait SliceOwned: Slice {
443 /// Index the slice, returning an owned value.
444 fn get_owned(&self, index: usize) -> Option<Self::Output>;
445
446 /// Return an iterator over arrays covering consecutive portions of the
447 /// slice.
448 ///
449 /// Analagous to [`slice::array_chunks`].
450 ///
451 /// # Panics
452 ///
453 /// If `N == 0`, panics.
454 ///
455 /// # Examples
456 ///
457 /// ```rust
458 /// # use slice_utils::{SliceOwned};
459 /// let slice = [1, 2, 3, 4, 5];
460 /// let mut iter = slice.array_chunks::<2>();
461 ///
462 /// assert_eq!(iter.next(), Some([1, 2]));
463 /// assert_eq!(iter.next(), Some([3, 4]));
464 /// assert!(iter.next().is_none());
465 /// assert_eq!(iter.remainder(), [5]);
466 /// ```
467 fn array_chunks<const N: usize>(self) -> ArrayChunksOwned<Self, N>
468 where
469 Self: Sized,
470 {
471 ArrayChunksOwned::new(self)
472 }
473
474 /// Return a slice/iterator over arrays covering overlapping portions of the
475 /// slice.
476 ///
477 /// Analagous to [`slice::array_windows`].
478 ///
479 /// # Panics
480 ///
481 /// If `N == 0`, panics.
482 ///
483 /// # Examples
484 ///
485 /// ```rust
486 /// # use slice_utils::{SliceOwned};
487 /// let slice = [1, 2, 3, 4, 5];
488 /// let mut windows = slice.array_windows::<3>();
489 ///
490 /// assert_eq!(windows.next(), Some([1, 2, 3]));
491 /// assert_eq!(windows.next(), Some([2, 3, 4]));
492 /// assert_eq!(windows.next(), Some([3, 4, 5]));
493 /// assert!(windows.next().is_none());
494 /// assert_eq!(windows.get_owned(1), Some([2, 3, 4]));
495 /// ```
496 fn array_windows<const N: usize>(self) -> ArrayWindowsOwned<Self, N>
497 where
498 Self: Sized,
499 {
500 ArrayWindowsOwned::new(self)
501 }
502
503 /// Return an iterator over slices covering consecutive portions of the
504 /// slice.
505 ///
506 /// Analagous to [`slice::chunks`].
507 ///
508 /// # Panics
509 ///
510 /// If `size == 0`, panics.
511 ///
512 /// # Examples
513 ///
514 /// ```rust
515 /// # use slice_utils::SliceOwned;
516 /// let slice = [1, 2, 3, 4, 5];
517 /// let mut iter = slice.chunks(2);
518 ///
519 /// assert_eq!(iter.next().unwrap(), [1, 2]);
520 /// assert_eq!(iter.next().unwrap(), [3, 4]);
521 /// assert_eq!(iter.next().unwrap(), [5]);
522 /// assert!(iter.next().is_none());
523 /// ```
524 fn chunks(&self, size: usize) -> ChunksOwned<Self> {
525 ChunksOwned::new(self, size)
526 }
527
528 /// Call a closure on index, returning a new type.
529 ///
530 /// Analagous to [`Iterator::map`].
531 ///
532 /// # Examples
533 ///
534 /// ```rust
535 /// # use slice_utils::SliceOwned;
536 /// let slice = [0, 1, 2].map(|x| x != 0);
537 /// assert_eq!(slice, [false, true, true]);
538 /// ```
539 fn map<F: Fn(Self::Output) -> R, R>(self, f: F) -> MapOwned<Self, F>
540 where
541 Self: Sized,
542 {
543 MapOwned(self, f)
544 }
545
546 /// Creates an iterator over the slice.
547 ///
548 /// # Examples
549 ///
550 /// ```rust
551 /// # use slice_utils::{Slice, SliceOwned};
552 /// let slice = [1, 2].chain([3]);
553 /// let mut iter = slice.iter();
554 /// assert_eq!(iter.next(), Some(1));
555 /// assert_eq!(iter.next(), Some(2));
556 /// assert_eq!(iter.next(), Some(3));
557 /// assert!(iter.next().is_none());
558 /// ```
559 fn iter(self) -> IterOwned<Self>
560 where
561 Self: Sized,
562 {
563 IterOwned::new(self)
564 }
565
566 /// Try to collect the slice into an array, failing if the lengths don't
567 /// match up.
568 ///
569 /// # Examples
570 ///
571 /// ```rust
572 /// # use slice_utils::SliceOwned;
573 /// let slice = [1, 2, 3].map(|i| i * i);
574 /// let arr: [i32; 3] = slice.try_array().unwrap();
575 /// assert_eq!(arr, [1, 4, 9]);
576 /// ```
577 fn try_array<const N: usize>(&self) -> Option<[Self::Output; N]> {
578 if self.len() != N {
579 None
580 } else {
581 Some(core::array::from_fn(|i| self.get_owned(i).unwrap()))
582 }
583 }
584
585 /// Return a slice/iterator over slices covering overlapping portions of the
586 /// slice.
587 ///
588 /// Analagous to [`slice::windows`].
589 ///
590 /// # Panics
591 ///
592 /// If `size == 0`, panics.
593 ///
594 /// # Examples
595 ///
596 /// ```rust
597 /// # use slice_utils::SliceOwned;
598 /// let slice = [1, 2, 3, 4, 5];
599 /// let mut windows = slice.windows(3);
600 ///
601 /// assert_eq!(windows.next().unwrap(), [1, 2, 3]);
602 /// assert_eq!(windows.next().unwrap(), [2, 3, 4]);
603 /// assert_eq!(windows.next().unwrap(), [3, 4, 5]);
604 /// assert!(windows.next().is_none());
605 /// assert_eq!(windows.get_owned(1).unwrap(), [2, 3, 4]);
606 /// ```
607 fn windows(&self, size: usize) -> WindowsOwned<Self> {
608 WindowsOwned::new(self, size)
609 }
610
611 /// Zip two slices into a single slice, where indexing returns a tuple of
612 /// their items.
613 ///
614 /// Analagous to [`Iterator::zip`].
615 ///
616 /// # Examples
617 ///
618 /// ```rust
619 /// # use slice_utils::SliceOwned;
620 /// let a = [1, 2, 3];
621 /// let b = [4, 5, 6, 7];
622 /// let slice = a.zip(b);
623 ///
624 /// assert_eq!(
625 /// slice,
626 /// [
627 /// (1, 4),
628 /// (2, 5),
629 /// (3, 6),
630 /// ]
631 /// );
632 /// ```
633 fn zip<O: SliceOwned>(self, other: O) -> Zip<Self, O>
634 where
635 Self: Sized,
636 {
637 Zip(self, other)
638 }
639
640 /// Collect the slice into a `Vec`. Only available on feature `std`.
641 ///
642 /// Analagous to [`Iterator::collect`].
643 #[cfg(feature = "std")]
644 fn collect(&self) -> Vec<Self::Output> {
645 let mut v = Vec::with_capacity(self.len());
646 for i in 0..self.len() {
647 v.push(self.get_owned(i).unwrap());
648 }
649
650 v
651 }
652}
653
654/// A [slice](SliceBorrowed) whose backing is contiguous in memory.
655///
656/// See also [`ContiguousMut`]. Note that there's no `ContiguousOwned`: this is
657/// because Rust does not (yet) allow you to use associated constants in const
658/// operations, so there is no way to return an array.
659///
660/// The alternative would be an "owning borrow", where you return a slice that
661/// has permission to treat the referenced data like it owns it (because it
662/// does), but those are still imperfect.
663pub trait ContiguousBorrowed: SliceBorrowed + Unique {
664 /// Get the contiguous backing of this slice.
665 ///
666 /// The returned slice should behave exactly like this one, e.g.
667 /// `self.get(n) == self.contiguous().get(n)`.
668 ///
669 /// # Examples
670 ///
671 /// ```rust
672 /// # use slice_utils::ContiguousBorrowed;
673 /// let a = [1, 2, 3].to_vec();
674 /// let b = a.contiguous();
675 ///
676 /// assert_eq!(a, b);
677 /// ```
678 fn contiguous(&self) -> &[Self::Output];
679}
680
681/// A [slice](SliceMut) whose backing is contiguous in memory.
682///
683/// See also [`ContiguousBorrowed`].
684pub trait ContiguousMut: SliceMut + Unique {
685 /// Get a mutable reference to the contiguous backing of this slice.
686 ///
687 /// The returned slice should behave exactly like this one, e.g.
688 /// `self.get_mut(n) == self.contiguous_mut().get_mut(n)`.
689 ///
690 /// # Examples
691 ///
692 /// ```rust
693 /// # use slice_utils::ContiguousMut;
694 /// let mut a = [1, 2, 3].to_vec();
695 ///
696 /// let b = a.contiguous_mut();
697 /// b.copy_from_slice(&[4, 5, 6]); // method on core::slice
698 ///
699 /// assert_eq!(a, [4, 5, 6]);
700 /// ```
701 fn contiguous_mut(&mut self) -> &mut [Self::Output];
702}
703
704/// A marker trait confirming that two indices of a [`Slice`] will never alias.
705///
706/// # Safety
707///
708/// Two calls to `get_mut` (or any other index operation) with different indices
709/// must never return aliasing references.
710pub unsafe trait Unique {}
711
712/// A slice made by calling a closure on the index.
713///
714/// Analagous to [`core::iter::from_fn`].
715///
716/// # Examples
717///
718/// ```rust
719/// # use slice_utils::SliceOwned;
720/// let slice = slice_utils::from_fn(|i| Some(i * i), Some(5));
721/// assert_eq!(slice, [0, 1, 4, 9, 16]);
722/// ```
723pub fn from_fn<F, T>(f: F, len: Option<usize>) -> FromFn<F>
724where
725 F: Fn(usize) -> Option<T>,
726{
727 FromFn::new(f, len)
728}