Skip to content

Commit

Permalink
Use impl Trait syntax for function params
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Jan 29, 2024
1 parent 7deb1a3 commit ad3736c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
12 changes: 3 additions & 9 deletions src/from_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ where
{
/// Create array where each array element `T` is returned by the `f` call.
#[inline]
pub fn from_fn<F>(mut f: F) -> Self
where
F: FnMut(usize) -> T,
{
Self::try_from_fn::<Infallible, _>(|n| Ok(f(n))).expect("should never fail")
pub fn from_fn(mut f: impl FnMut(usize) -> T) -> Self {
Self::try_from_fn::<Infallible>(|n| Ok(f(n))).expect("should never fail")
}

/// Create array fallibly where each array element `T` is returned by the `f` call, or return
Expand All @@ -26,10 +23,7 @@ where
/// # Errors
///
/// Propagates the `E` type returned from the provided `F` in the event of error.
pub fn try_from_fn<E, F>(f: F) -> Result<Self, E>
where
F: FnMut(usize) -> Result<T, E>,
{
pub fn try_from_fn<E>(f: impl FnMut(usize) -> Result<T, E>) -> Result<Self, E> {
let mut array = Array::<MaybeUninit<T>, U>::uninit();
try_from_fn_erased(array.0.as_mut(), f)?;

Expand Down
4 changes: 2 additions & 2 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ fn from_fn() {

#[test]
fn try_from_fn() {
let array = Array::<u8, U6>::try_from_fn::<(), _>(|n| Ok((n + 1) as u8)).unwrap();
let array = Array::<u8, U6>::try_from_fn::<()>(|n| Ok((n + 1) as u8)).unwrap();
assert_eq!(array.as_slice(), EXAMPLE_SLICE);

let err = Array::<u8, U6>::try_from_fn::<&'static str, _>(|_| Err("err"))
let err = Array::<u8, U6>::try_from_fn::<&'static str>(|_| Err("err"))
.err()
.unwrap();

Expand Down

0 comments on commit ad3736c

Please sign in to comment.