1use crate::{
2 DBWeight,
3 algebra::{AddAssignByRef, AddByRef, HasZero},
4 declare_trait_object, declare_typed_trait_object,
5 dynamic::data::DataTyped,
6};
7
8use super::{Data, DataTrait, DataTraitTyped};
9
10pub trait Weight: Data {
14 fn set_zero(&mut self);
16
17 fn is_zero(&self) -> bool;
19
20 unsafe fn raw_add(&self, rhs: *const u8, result: *mut u8);
26
27 unsafe fn raw_add_assign(&mut self, rhs: *const u8);
33}
34
35impl<T: DBWeight> Weight for T {
36 fn set_zero(&mut self) {
37 *self = HasZero::zero();
38 }
39
40 fn is_zero(&self) -> bool {
41 HasZero::is_zero(self)
42 }
43
44 unsafe fn raw_add(&self, rhs: *const u8, result: *mut u8) {
45 unsafe { *(result as *mut Self) = AddByRef::add_by_ref(self, &*(rhs as *const Self)) }
46 }
47
48 unsafe fn raw_add_assign(&mut self, rhs: *const u8) {
49 unsafe { AddAssignByRef::add_assign_by_ref(self, &*(rhs as *const Self)) }
50 }
51}
52
53pub trait WeightTyped: Weight + DataTyped {}
58
59impl<T> WeightTyped for T where T: Weight + DataTyped {}
60
61pub trait WeightTrait: DataTrait + Weight {
63 fn add(&self, rhs: &Self, result: &mut Self) {
65 debug_assert_eq!(self.as_any().type_id(), rhs.as_any().type_id());
66 debug_assert_eq!(self.as_any().type_id(), result.as_any().type_id());
67
68 unsafe {
69 Weight::raw_add(
70 self,
71 rhs as *const Self as *const u8,
72 result as *mut Self as *mut u8,
73 )
74 }
75 }
76
77 fn add_assign(&mut self, rhs: &Self) {
79 debug_assert_eq!(self.as_any().type_id(), rhs.as_any().type_id());
80
81 unsafe { Weight::raw_add_assign(self, rhs as *const Self as *const u8) }
82 }
83}
84
85impl<Trait: ?Sized> WeightTrait for Trait where Trait: DataTrait + Weight {}
86
87pub trait WeightTraitTyped: WeightTrait + DataTraitTyped {}
94
95impl<Trait: ?Sized> WeightTraitTyped for Trait where Trait: WeightTrait + DataTraitTyped {}
96
97declare_trait_object!(DynWeight<> = dyn Weight<>
98where
99);
100
101declare_typed_trait_object!(DynWeightTyped<T> = dyn WeightTyped<Type = T>
102 [T]
103where
104 T: DBWeight,
105);