From 51827589d2a841730254893dd2948f2c8e9a99f7 Mon Sep 17 00:00:00 2001 From: Alex Vlasov Date: Thu, 17 Jun 2021 18:13:28 +0200 Subject: [PATCH 1/2] Initial implementation --- library/core/src/array/mod.rs | 16 ++++++++++++++++ library/core/tests/array.rs | 6 ++++++ library/core/tests/lib.rs | 1 + 3 files changed, 23 insertions(+) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 37af3557fdd51..7c18a8467fad5 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -389,6 +389,22 @@ array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T #[lang = "array"] impl [T; N] { + /// Returns the number of elements in the array. + /// + /// # Examples + /// + /// ``` + /// #![feature(array_len)] + /// let a: [usize; 3] = [1, 2, 3]; + /// assert_eq!(a.len(), 3); + /// ``` + #[doc(alias = "length")] + #[unstable(feature = "array_len", issue = "none")] + #[inline] + pub const fn len(&self) -> usize { + N + } + /// Returns an array of the same size as `self`, with function `f` applied to each element /// in order. /// diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index 0ae625bdb68c6..0fe59c795d8d4 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -288,6 +288,12 @@ fn empty_array_is_always_default() { let _arr = <[DoesNotImplDefault; 0]>::default(); } +#[test] +fn array_len() { + let a = [1, 2, 3]; + assert_eq!(a.len(), 3); +} + #[test] fn array_map() { let a = [1, 2, 3]; diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 16051b3bc36c7..5527e1019792a 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -1,6 +1,7 @@ #![feature(alloc_layout_extra)] #![feature(array_chunks)] #![feature(array_methods)] +#![feature(array_len)] #![feature(array_map)] #![feature(array_windows)] #![feature(bool_to_option)] From 189342d646acdc2e4d4b203a590c397dae4d0b83 Mon Sep 17 00:00:00 2001 From: Alex Vlasov Date: Sun, 20 Jun 2021 15:01:04 +0200 Subject: [PATCH 2/2] mark as insta-stable-ish --- library/core/src/array/mod.rs | 5 +++-- library/core/tests/lib.rs | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 7c18a8467fad5..7dec9328eebd3 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -394,12 +394,13 @@ impl [T; N] { /// # Examples /// /// ``` - /// #![feature(array_len)] /// let a: [usize; 3] = [1, 2, 3]; /// assert_eq!(a.len(), 3); /// ``` + #[cfg(not(bootstrap))] #[doc(alias = "length")] - #[unstable(feature = "array_len", issue = "none")] + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "array_len", since = "1.53.0")] #[inline] pub const fn len(&self) -> usize { N diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 5527e1019792a..16051b3bc36c7 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -1,7 +1,6 @@ #![feature(alloc_layout_extra)] #![feature(array_chunks)] #![feature(array_methods)] -#![feature(array_len)] #![feature(array_map)] #![feature(array_windows)] #![feature(bool_to_option)]