mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 12:17:58 +00:00
Derive no bound macros (to be also used in pallet macro) (#7280)
* derive no bound macros * explicit different variant for partialeq * fix ui for 1.47 * Apply suggestions from code review Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * move test to frame-support-test * renames, code organization and remove expect as suggested * better doc * remove DebugStripped introduce RuntimeDebugNoBound * rename * fix test * fix ui test * fix line width * Update frame/support/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Update frame/support/procedural/src/clone_no_bound.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * fix confusing dead code Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
87c18598fc
commit
7ca9baf9b6
@@ -0,0 +1,170 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Tests for DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, and RuntimeDebugNoBound
|
||||
|
||||
use frame_support::{DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound};
|
||||
|
||||
#[derive(RuntimeDebugNoBound)]
|
||||
struct Unnamed(u64);
|
||||
|
||||
#[test]
|
||||
fn runtime_debug_no_bound_display_correctly() {
|
||||
// This test is not executed without std
|
||||
assert_eq!(format!("{:?}", Unnamed(1)), "Unnamed(1)");
|
||||
}
|
||||
|
||||
trait Trait {
|
||||
type C: std::fmt::Debug + Clone + Eq + PartialEq;
|
||||
}
|
||||
|
||||
struct Runtime;
|
||||
struct ImplNone;
|
||||
|
||||
impl Trait for Runtime {
|
||||
type C = u32;
|
||||
}
|
||||
|
||||
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound)]
|
||||
struct StructNamed<T: Trait, U, V> {
|
||||
a: u32,
|
||||
b: u64,
|
||||
c: T::C,
|
||||
phantom: core::marker::PhantomData<(U, V)>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_struct_named() {
|
||||
let a_1 = StructNamed::<Runtime, ImplNone, ImplNone> {
|
||||
a: 1,
|
||||
b: 2,
|
||||
c: 3,
|
||||
phantom: Default::default(),
|
||||
};
|
||||
|
||||
let a_2 = a_1.clone();
|
||||
assert_eq!(a_2.a, 1);
|
||||
assert_eq!(a_2.b, 2);
|
||||
assert_eq!(a_2.c, 3);
|
||||
assert_eq!(a_2, a_1);
|
||||
assert_eq!(
|
||||
format!("{:?}", a_1),
|
||||
String::from("StructNamed { a: 1, b: 2, c: 3, phantom: PhantomData }")
|
||||
);
|
||||
|
||||
let b = StructNamed::<Runtime, ImplNone, ImplNone> {
|
||||
a: 1,
|
||||
b: 2,
|
||||
c: 4,
|
||||
phantom: Default::default(),
|
||||
};
|
||||
|
||||
assert!(b != a_1);
|
||||
}
|
||||
|
||||
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound)]
|
||||
struct StructUnnamed<T: Trait, U, V>(u32, u64, T::C, core::marker::PhantomData<(U, V)>);
|
||||
|
||||
#[test]
|
||||
fn test_struct_unnamed() {
|
||||
let a_1 = StructUnnamed::<Runtime, ImplNone, ImplNone>(
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
let a_2 = a_1.clone();
|
||||
assert_eq!(a_2.0, 1);
|
||||
assert_eq!(a_2.1, 2);
|
||||
assert_eq!(a_2.2, 3);
|
||||
assert_eq!(a_2, a_1);
|
||||
assert_eq!(
|
||||
format!("{:?}", a_1),
|
||||
String::from("StructUnnamed(1, 2, 3, PhantomData)")
|
||||
);
|
||||
|
||||
let b = StructUnnamed::<Runtime, ImplNone, ImplNone>(
|
||||
1,
|
||||
2,
|
||||
4,
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
assert!(b != a_1);
|
||||
}
|
||||
|
||||
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound)]
|
||||
enum Enum<T: Trait, U, V> {
|
||||
VariantUnnamed(u32, u64, T::C, core::marker::PhantomData<(U, V)>),
|
||||
VariantNamed {
|
||||
a: u32,
|
||||
b: u64,
|
||||
c: T::C,
|
||||
phantom: core::marker::PhantomData<(U, V)>,
|
||||
},
|
||||
VariantUnit,
|
||||
VariantUnit2,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enum() {
|
||||
type TestEnum = Enum::<Runtime, ImplNone, ImplNone>;
|
||||
let variant_0 = TestEnum::VariantUnnamed(1, 2, 3, Default::default());
|
||||
let variant_0_bis = TestEnum::VariantUnnamed(1, 2, 4, Default::default());
|
||||
let variant_1 = TestEnum::VariantNamed { a: 1, b: 2, c: 3, phantom: Default::default() };
|
||||
let variant_1_bis = TestEnum::VariantNamed { a: 1, b: 2, c: 4, phantom: Default::default() };
|
||||
let variant_2 = TestEnum::VariantUnit;
|
||||
let variant_3 = TestEnum::VariantUnit2;
|
||||
|
||||
assert!(variant_0 != variant_0_bis);
|
||||
assert!(variant_1 != variant_1_bis);
|
||||
assert!(variant_0 != variant_1);
|
||||
assert!(variant_0 != variant_2);
|
||||
assert!(variant_0 != variant_3);
|
||||
assert!(variant_1 != variant_0);
|
||||
assert!(variant_1 != variant_2);
|
||||
assert!(variant_1 != variant_3);
|
||||
assert!(variant_2 != variant_0);
|
||||
assert!(variant_2 != variant_1);
|
||||
assert!(variant_2 != variant_3);
|
||||
assert!(variant_3 != variant_0);
|
||||
assert!(variant_3 != variant_1);
|
||||
assert!(variant_3 != variant_2);
|
||||
|
||||
assert!(variant_0.clone() == variant_0);
|
||||
assert!(variant_1.clone() == variant_1);
|
||||
assert!(variant_2.clone() == variant_2);
|
||||
assert!(variant_3.clone() == variant_3);
|
||||
|
||||
assert_eq!(
|
||||
format!("{:?}", variant_0),
|
||||
String::from("Enum::VariantUnnamed(1, 2, 3, PhantomData)"),
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{:?}", variant_1),
|
||||
String::from("Enum::VariantNamed { a: 1, b: 2, c: 3, phantom: PhantomData }"),
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{:?}", variant_2),
|
||||
String::from("Enum::VariantUnit"),
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{:?}", variant_3),
|
||||
String::from("Enum::VariantUnit2"),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#[rustversion::attr(not(stable), ignore)]
|
||||
#[test]
|
||||
fn derive_no_bound_ui() {
|
||||
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
|
||||
std::env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
|
||||
|
||||
let t = trybuild::TestCases::new();
|
||||
t.compile_fail("tests/derive_no_bound_ui/*.rs");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
trait Trait {
|
||||
type C;
|
||||
}
|
||||
|
||||
#[derive(frame_support::CloneNoBound)]
|
||||
struct Foo<T: Trait> {
|
||||
c: T::C,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,7 @@
|
||||
error[E0277]: the trait bound `<T as Trait>::C: std::clone::Clone` is not satisfied
|
||||
--> $DIR/clone.rs:7:2
|
||||
|
|
||||
7 | c: T::C,
|
||||
| ^ the trait `std::clone::Clone` is not implemented for `<T as Trait>::C`
|
||||
|
|
||||
= note: required by `std::clone::Clone::clone`
|
||||
@@ -0,0 +1,10 @@
|
||||
trait Trait {
|
||||
type C;
|
||||
}
|
||||
|
||||
#[derive(frame_support::DebugNoBound)]
|
||||
struct Foo<T: Trait> {
|
||||
c: T::C,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,8 @@
|
||||
error[E0277]: `<T as Trait>::C` doesn't implement `std::fmt::Debug`
|
||||
--> $DIR/debug.rs:7:2
|
||||
|
|
||||
7 | c: T::C,
|
||||
| ^ `<T as Trait>::C` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
|
||||
|
|
||||
= help: the trait `std::fmt::Debug` is not implemented for `<T as Trait>::C`
|
||||
= note: required for the cast to the object type `dyn std::fmt::Debug`
|
||||
@@ -0,0 +1,10 @@
|
||||
trait Trait {
|
||||
type C;
|
||||
}
|
||||
|
||||
#[derive(frame_support::EqNoBound)]
|
||||
struct Foo<T: Trait> {
|
||||
c: T::C,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,7 @@
|
||||
error[E0277]: can't compare `Foo<T>` with `Foo<T>`
|
||||
--> $DIR/eq.rs:6:8
|
||||
|
|
||||
6 | struct Foo<T: Trait> {
|
||||
| ^^^ no implementation for `Foo<T> == Foo<T>`
|
||||
|
|
||||
= help: the trait `std::cmp::PartialEq` is not implemented for `Foo<T>`
|
||||
@@ -0,0 +1,10 @@
|
||||
trait Trait {
|
||||
type C;
|
||||
}
|
||||
|
||||
#[derive(frame_support::PartialEqNoBound)]
|
||||
struct Foo<T: Trait> {
|
||||
c: T::C,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,7 @@
|
||||
error[E0369]: binary operation `==` cannot be applied to type `<T as Trait>::C`
|
||||
--> $DIR/partial_eq.rs:7:2
|
||||
|
|
||||
7 | c: T::C,
|
||||
| ^
|
||||
|
|
||||
= note: the trait `std::cmp::PartialEq` is not implemented for `<T as Trait>::C`
|
||||
Reference in New Issue
Block a user