Skip to content

Commit

Permalink
Added default impl of from_fn; added try_from_fn to changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
rozbb committed Nov 1, 2023
1 parent 2f7f15a commit 8c6466f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions hybrid-array/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Pending

- Added `serde` feature - implements `Serialize` and `Deserialize` for `Array`
- Added `ArrayExt::try_from_fn`

## 0.1.0 (2022-05-07)
- Initial release
25 changes: 17 additions & 8 deletions hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,17 +576,23 @@ pub trait ArrayOps<T, const N: usize>:

/// Extension trait with helper functions for core arrays.
pub trait ArrayExt<T>: Sized {
/// Create array using the given callback function for each element.
fn from_fn<F>(cb: F) -> Self
where
F: FnMut(usize) -> T;

/// Try to create an array using the given callback function for each element. Returns an error
/// if any one of the calls errors
fn try_from_fn<F, E>(cb: F) -> Result<Self, E>
where
F: FnMut(usize) -> Result<T, E>;

/// Create array using the given callback function for each element.
fn from_fn<F>(mut cb: F) -> Self
where
F: FnMut(usize) -> T,
{
// Turn the ordinary callback into a Result callback that always returns Ok
let wrapped_cb = |x| Result::<T, ::core::convert::Infallible>::Ok(cb(x));
// Now use the try_from version of this method
Self::try_from_fn(wrapped_cb).unwrap()
}

/// Create array from a slice, returning [`TryFromSliceError`] if the slice
/// length does not match the array length.
fn from_slice(slice: &[T]) -> Result<Self, TryFromSliceError>
Expand All @@ -595,6 +601,9 @@ pub trait ArrayExt<T>: Sized {
}

impl<T, const N: usize> ArrayExt<T> for [T; N] {
// TODO: Eventually remove this and just use the default implementation. It's still
// here because the current Self::try_from_fn might be doing multiple passes over the data,
// depending on optimizations. Thus, this may be faster.
fn from_fn<F>(mut cb: F) -> Self
where
F: FnMut(usize) -> T,
Expand Down Expand Up @@ -628,8 +637,8 @@ impl<T, const N: usize> ArrayExt<T> for [T; N] {
Ok(val) => {
elem.write(val);
}
Err(e) => {
err_info = Some((idx, e));
Err(err) => {
err_info = Some((idx, err));
}
}
}
Expand All @@ -646,7 +655,7 @@ impl<T, const N: usize> ArrayExt<T> for [T; N] {
// If we've made it this far, all the elements have been written. Convert the uninitialized
// array to an initialized array
// TODO: Replace this map with MaybeUninit::array_assume_init() once it stabilizes
let arr = arr.map(|elem: MaybeUninit<T>| unsafe { elem.assume_init() });
let arr = arr.map(|v: MaybeUninit<T>| unsafe { v.assume_init() });
Ok(arr)
}

Expand Down

0 comments on commit 8c6466f

Please sign in to comment.