From 94762032bdf556fb01c01f36a722abf94b27cfb8 Mon Sep 17 00:00:00 2001 From: Arthur Chaloin Date: Sun, 14 Sep 2025 17:04:21 +0200 Subject: [PATCH] Implement From conversion for non-empty hardcoded arrays --- src/lib.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 07465bb..93de30e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1056,6 +1056,26 @@ impl Extend for NonEmpty { } } +impl From<[T; S]> for NonEmpty { + fn from(array: [T; S]) -> Self { + const { + if S == 0 { + panic!("tried to construct NonEmpty from an empty array") + } + } + + let mut iter = array.into_iter(); + + // SAFETY: we know that S is not 0, so we can safely unwrap + let head = iter.next().unwrap(); + + NonEmpty { + head, + tail: iter.collect(), + } + } +} + #[cfg(feature = "serialize")] pub mod serialize { use core::{convert::TryFrom, fmt};