Skip to content

Commit 4bcc5b2

Browse files
authored
Make Const* types Clone+Default and fix Debug (#744)
* Impl Default, Clone and Debug Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Add tests Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Bump to 0.1.6 Signed-off-by: Oliver Tale-Yazdi <[email protected]> * std-guard 'format' test Signed-off-by: Oliver Tale-Yazdi <[email protected]> --------- Signed-off-by: Oliver Tale-Yazdi <[email protected]>
1 parent 910097e commit 4bcc5b2

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

bounded-collections/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The format is based on [Keep a Changelog].
44

55
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/
66

7+
## [0.1.6] - 2023-04-27
8+
- Added `Clone` and `Default` derive to the `impl_const_get!` macro and thereby all `Const*` types.
9+
- Fixed `Debug` impl for `impl_const_get!` and all `Const*` types to also print the value and not just the type name.
10+
711
## [0.1.5] - 2023-02-13
812
- Fixed `Hash` impl (previously it could not be used in practice, because the size bound was required to also implement `Hash`).
913

bounded-collections/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bounded-collections"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
authors = ["Parity Technologies <[email protected]>"]
55
license = "MIT OR Apache-2.0"
66
homepage = "https://github.com/paritytech/parity-common"

bounded-collections/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub mod bounded_btree_set;
1818
pub mod bounded_vec;
1919
pub mod weak_bounded_vec;
2020

21+
mod test;
22+
2123
pub use bounded_btree_map::BoundedBTreeMap;
2224
pub use bounded_btree_set::BoundedBTreeSet;
2325
pub use bounded_vec::{BoundedSlice, BoundedVec};
@@ -58,8 +60,15 @@ impl<T: Default> Get<T> for GetDefault {
5860
macro_rules! impl_const_get {
5961
($name:ident, $t:ty) => {
6062
/// Const getter for a basic type.
61-
#[cfg_attr(feature = "std", derive(core::fmt::Debug))]
63+
#[derive(Default, Clone)]
6264
pub struct $name<const T: $t>;
65+
66+
#[cfg(feature = "std")]
67+
impl<const T: $t> core::fmt::Debug for $name<T> {
68+
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
69+
fmt.write_str(&format!("{}<{}>", stringify!($name), T))
70+
}
71+
}
6372
#[cfg(not(feature = "std"))]
6473
impl<const T: $t> core::fmt::Debug for $name<T> {
6574
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {

bounded-collections/src/test.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2023 Parity Technologies
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Tests for the `bounded-collections` crate.
10+
11+
#![cfg(test)]
12+
13+
use crate::*;
14+
use core::fmt::Debug;
15+
16+
#[test]
17+
#[allow(path_statements)]
18+
fn const_impl_default_clone_debug() {
19+
struct ImplsDefault<T: Default + Clone + Debug>(T);
20+
21+
ImplsDefault::<ConstBool<true>>;
22+
ImplsDefault::<ConstBool<false>>;
23+
ImplsDefault::<ConstU8<255>>;
24+
ImplsDefault::<ConstU16<50>>;
25+
ImplsDefault::<ConstU32<10>>;
26+
ImplsDefault::<ConstU64<99>>;
27+
ImplsDefault::<ConstU128<100>>;
28+
ImplsDefault::<ConstI8<-127>>;
29+
ImplsDefault::<ConstI16<-50>>;
30+
ImplsDefault::<ConstI32<-10>>;
31+
ImplsDefault::<ConstI64<-99>>;
32+
ImplsDefault::<ConstI128<-100>>;
33+
}
34+
35+
#[test]
36+
#[cfg(feature = "std")]
37+
fn const_debug_fmt() {
38+
assert_eq!(format!("{:?}", ConstBool::<true> {}), "ConstBool<true>");
39+
assert_eq!(format!("{:?}", ConstBool::<false> {}), "ConstBool<false>");
40+
assert_eq!(format!("{:?}", ConstU8::<255> {}), "ConstU8<255>");
41+
assert_eq!(format!("{:?}", ConstU16::<50> {}), "ConstU16<50>");
42+
assert_eq!(format!("{:?}", ConstU32::<10> {}), "ConstU32<10>");
43+
assert_eq!(format!("{:?}", ConstU64::<99> {}), "ConstU64<99>");
44+
assert_eq!(format!("{:?}", ConstU128::<100> {}), "ConstU128<100>");
45+
assert_eq!(format!("{:?}", ConstI8::<-127> {}), "ConstI8<-127>");
46+
assert_eq!(format!("{:?}", ConstI16::<-50> {}), "ConstI16<-50>");
47+
assert_eq!(format!("{:?}", ConstI32::<-10> {}), "ConstI32<-10>");
48+
assert_eq!(format!("{:?}", ConstI64::<-99> {}), "ConstI64<-99>");
49+
assert_eq!(format!("{:?}", ConstI128::<-100> {}), "ConstI128<-100>");
50+
}

0 commit comments

Comments
 (0)