From d21b7d2735efd07cf7695486f65dc6690bd64311 Mon Sep 17 00:00:00 2001 From: Emil Hofstetter Date: Sun, 12 Feb 2023 23:33:02 -0500 Subject: [PATCH 1/2] add try_from_iter --- library/core/src/array/mod.rs | 29 +++++++++++++++++++++++++++++ library/core/tests/array.rs | 12 ++++++++++++ library/core/tests/lib.rs | 1 + 3 files changed, 42 insertions(+) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 5decd7d5a65bb..1d86cabd76aba 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -959,3 +959,32 @@ where let mut map = iter.map(NeverShortCircuit); try_collect_into_array(&mut map).map(|NeverShortCircuit(arr)| arr) } + +/// Tries to create an array `[T; N]` from the items of `iter`. If and only if `iter` holds +/// `>= N` items it returns `OK([T; N])`. Otherwise, `Err(IntoIter)` is returned. +/// +/// # Arguments +/// +/// * `iter`: Type that implements `IntoIterator` where `Items = T`. +/// +/// # Example +/// +/// ```rust +/// #![feature(try_from_iter)] +/// let vec = vec![0, 1, 2, 3]; +/// let array = core::array::try_from_iter::<_, 4>(vec); +/// assert!(array.is_ok()); +/// assert_eq!(array.unwrap(), [0, 1, 2, 3]); +/// +/// let vec = vec![0, 1, 2]; +/// let array = core::array::try_from_iter::<_, 4>(vec); +/// assert!(array.is_err()); +/// ``` +#[unstable(feature = "try_from_iter", issue = "none")] +#[inline] +pub fn try_from_iter(iter: I) -> Result<[I::Item; N], IntoIter> +where + I: IntoIterator, +{ + iter.into_iter().next_chunk() +} diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index f268fe3ae7ba8..580f5b4e8839b 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -700,3 +700,15 @@ fn array_into_iter_rfold() { let s = it.rfold(10, |a, b| 10 * a + b); assert_eq!(s, 10432); } + +#[test] +fn array_try_fill_from() { + let vec = vec![0, 1, 2, 3]; + let array = core::array::try_from_iter::<_, 4>(vec); + assert!(array.is_ok()); + assert_eq!(array.unwrap(), [0, 1, 2, 3]); + + let vec = vec![0, 1, 2]; + let array = core::array::try_from_iter::<_, 4>(vec); + assert!(array.is_err()); +} diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 42a26ae1675c1..17e57abed0c87 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -108,6 +108,7 @@ #![feature(utf8_chunks)] #![feature(is_ascii_octdigit)] #![feature(get_many_mut)] +#![feature(try_from_iter)] #![deny(unsafe_op_in_unsafe_fn)] #![deny(fuzzy_provenance_casts)] From 7f856a332004fa0cfcf56b819b429442fcef7fc0 Mon Sep 17 00:00:00 2001 From: emilHof <95590295+emilHof@users.noreply.github.com> Date: Mon, 13 Feb 2023 11:15:56 -0500 Subject: [PATCH 2/2] Add new line to EOF --- library/core/tests/array.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index d22d63ee3aea7..ea32e0f967f7f 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -736,4 +736,4 @@ fn array_map_drops_unmapped_elements_on_panic() { assert!(success.is_err()); assert_eq!(counter.load(Ordering::SeqCst), MAX); } -} \ No newline at end of file +}