Skip to content

Add try_from_iter (initialize Result<[T; N], E> from Result<T> iterators) #33

@l0calh05t

Description

@l0calh05t

Iterator::collect can collect an Iterator<Item = Result<T, E>> into a Result<Vec<T>, E>. It would be great if there was an array_init::from_iter equivalent to initialize a Result<[T;N], E> using such an iterator (maybe called try_from_iter).

Since from_iter is implemented on top of try_array_init_impl I think this shouldn't be too complex to realize. However, since an iterator that is too short can't produce an arbitrary error E, the return type may have to be

  • a Result<[T; N], Option<E>>; probably not good as Option doesn't implement std::error::Error
  • a Result<Option<[T; N]>, E>; needs a double unwrap if the iterator is too short, but probably a bit better
  • a Result<[T; N], ArrayInitError<E>>, where ArrayInitError<E> can either hold a length error or an E, and implements std::error::Error (if E implements it); this is probably the most complex to implement and increases the API surface the most

If you have a preference (or a different suggestion), I should be able to cobble together a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions