diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2020-10-31 09:49:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-31 09:49:39 +0100 |
| commit | 3601f9d40bdcd8bb7e3057e1d66c0b187e40ed9e (patch) | |
| tree | 14d072110d5b92b980cd0d653dbb702b6f8986c8 | |
| parent | 841f0e7f2cd90747d6a7d0e699fac342cc6ee92b (diff) | |
| parent | 307cc11bebe930fa13dec86408e8ae6dbc04a037 (diff) | |
| download | rust-3601f9d40bdcd8bb7e3057e1d66c0b187e40ed9e.tar.gz rust-3601f9d40bdcd8bb7e3057e1d66c0b187e40ed9e.zip | |
Rollup merge of #78581 - a1phyr:const_btree_more, r=dtolnay
Constantify more BTreeMap and BTreeSet functions Just because we can: - `BTreeMap::len` - `BTreeMap::is_empty` - `BTreeSet::len` - `BTreeSet::is_empty` Note that I put the `const` under `const_btree_new`, because I don't think their is a need to create another feature flag for that. cc #71835
| -rw-r--r-- | library/alloc/src/collections/btree/map.rs | 6 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/map/tests.rs | 7 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/set.rs | 6 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/set/tests.rs | 7 |
4 files changed, 22 insertions, 4 deletions
diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 2704aab3229..07c23d29e20 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -2188,7 +2188,8 @@ impl<K, V> BTreeMap<K, V> { /// assert_eq!(a.len(), 1); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn len(&self) -> usize { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn len(&self) -> usize { self.length } @@ -2207,7 +2208,8 @@ impl<K, V> BTreeMap<K, V> { /// assert!(!a.is_empty()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_empty(&self) -> bool { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn is_empty(&self) -> bool { self.len() == 0 } diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index adb94972f5b..09aabdcd0fb 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -1527,6 +1527,13 @@ fn test_send() { } } +#[allow(dead_code)] +fn test_const() { + const MAP: &'static BTreeMap<(), ()> = &BTreeMap::new(); + const LEN: usize = MAP.len(); + const IS_EMPTY: bool = MAP.is_empty(); +} + #[test] fn test_occupied_entry_key() { let mut a = BTreeMap::new(); diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 3ad74969bec..684019f8f5f 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -950,7 +950,8 @@ impl<T> BTreeSet<T> { /// assert_eq!(v.len(), 1); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn len(&self) -> usize { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn len(&self) -> usize { self.map.len() } @@ -967,7 +968,8 @@ impl<T> BTreeSet<T> { /// assert!(!v.is_empty()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_empty(&self) -> bool { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn is_empty(&self) -> bool { self.len() == 0 } } diff --git a/library/alloc/src/collections/btree/set/tests.rs b/library/alloc/src/collections/btree/set/tests.rs index 92674357282..2069cde4dba 100644 --- a/library/alloc/src/collections/btree/set/tests.rs +++ b/library/alloc/src/collections/btree/set/tests.rs @@ -15,6 +15,13 @@ fn test_clone_eq() { assert_eq!(m.clone(), m); } +#[allow(dead_code)] +fn test_const() { + const SET: &'static BTreeSet<()> = &BTreeSet::new(); + const LEN: usize = SET.len(); + const IS_EMPTY: bool = SET.is_empty(); +} + #[test] fn test_iter_min_max() { let mut a = BTreeSet::new(); |
