|
| 1 | +// Test several functions can be used for constants |
| 2 | +// 1. Vec::new() |
| 3 | +// 2. String::new() |
| 4 | +// 3. BTreeMap::new() |
| 5 | +// 4. BTreeSet::new() |
| 6 | + |
| 7 | +#[allow(dead_code)] |
| 8 | +pub const MY_VEC: Vec<usize> = Vec::new(); |
| 9 | + |
| 10 | +#[allow(dead_code)] |
| 11 | +pub const MY_STRING: String = String::new(); |
| 12 | + |
| 13 | +// FIXME remove this struct once we put `K: ?const Ord` on BTreeMap::new. |
| 14 | +#[derive(PartialEq, Eq, PartialOrd)] |
| 15 | +pub struct MyType; |
| 16 | + |
| 17 | +impl const Ord for MyType { |
| 18 | + fn cmp(&self, _: &Self) -> Ordering { |
| 19 | + Ordering::Equal |
| 20 | + } |
| 21 | + |
| 22 | + fn max(self, _: Self) -> Self { |
| 23 | + Self |
| 24 | + } |
| 25 | + |
| 26 | + fn min(self, _: Self) -> Self { |
| 27 | + Self |
| 28 | + } |
| 29 | + |
| 30 | + fn clamp(self, _: Self, _: Self) -> Self { |
| 31 | + Self |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +use core::cmp::Ordering; |
| 36 | +use std::collections::{BTreeMap, BTreeSet}; |
| 37 | + |
| 38 | +pub const MY_BTREEMAP: BTreeMap<MyType, MyType> = BTreeMap::new(); |
| 39 | +pub const MAP: &'static BTreeMap<MyType, MyType> = &MY_BTREEMAP; |
| 40 | +pub const MAP_LEN: usize = MAP.len(); |
| 41 | +pub const MAP_IS_EMPTY: bool = MAP.is_empty(); |
| 42 | + |
| 43 | +pub const MY_BTREESET: BTreeSet<MyType> = BTreeSet::new(); |
| 44 | +pub const SET: &'static BTreeSet<MyType> = &MY_BTREESET; |
| 45 | +pub const SET_LEN: usize = SET.len(); |
| 46 | +pub const SET_IS_EMPTY: bool = SET.is_empty(); |
| 47 | + |
| 48 | +#[test] |
| 49 | +fn test_const() { |
| 50 | + assert_eq!(MAP_LEN, 0); |
| 51 | + assert_eq!(SET_LEN, 0); |
| 52 | + assert!(MAP_IS_EMPTY && SET_IS_EMPTY) |
| 53 | +} |
0 commit comments