@@ -23,6 +23,21 @@ generate_fam_struct_impl!(
2323 KVM_MAX_CPUID_ENTRIES
2424) ;
2525
26+ // Implement the PartialEq trait for kvm_cpuid2.
27+ //
28+ // Note:
29+ // This PartialEq implementation should not be used directly, instead FamStructWrapper
30+ // should be used. FamStructWrapper<T> provides us with an PartialEq implementation,
31+ // and it will determine the entire contents of the entries array. But requires
32+ // type T to implement `Default + FamStruct + PartialEq`, so we implement PartialEq here
33+ // and only need to determine the header field.
34+ impl PartialEq for kvm_cpuid2 {
35+ fn eq ( & self , other : & kvm_cpuid2 ) -> bool {
36+ // No need to call entries's eq, FamStructWrapper's PartialEq will do it for you
37+ self . nent == other. nent && self . padding == other. padding
38+ }
39+ }
40+
2641/// Wrapper over the `kvm_cpuid2` structure.
2742///
2843/// The `kvm_cpuid2` structure contains a flexible array member. For details check the
@@ -42,6 +57,14 @@ generate_fam_struct_impl!(
4257 KVM_MAX_MSR_ENTRIES
4358) ;
4459
60+ // Implement the PartialEq trait for kvm_msrs.
61+ impl PartialEq for kvm_msrs {
62+ fn eq ( & self , other : & kvm_msrs ) -> bool {
63+ // No need to call entries's eq, FamStructWrapper's PartialEq will do it for you
64+ self . nmsrs == other. nmsrs && self . pad == other. pad
65+ }
66+ }
67+
4568/// Wrapper over the `kvm_msrs` structure.
4669///
4770/// The `kvm_msrs` structure contains a flexible array member. For details check the
@@ -54,6 +77,14 @@ pub type Msrs = FamStructWrapper<kvm_msrs>;
5477// Implement the FamStruct trait for kvm_msr_list.
5578generate_fam_struct_impl ! ( kvm_msr_list, u32 , indices, u32 , nmsrs, KVM_MAX_MSR_ENTRIES ) ;
5679
80+ // Implement the PartialEq trait for kvm_msr_list.
81+ impl PartialEq for kvm_msr_list {
82+ fn eq ( & self , other : & kvm_msr_list ) -> bool {
83+ // No need to call entries's eq, FamStructWrapper's PartialEq will do it for you
84+ self . nmsrs == other. nmsrs
85+ }
86+ }
87+
5788/// Wrapper over the `kvm_msr_list` structure.
5889///
5990/// The `kvm_msr_list` structure contains a flexible array member. For details check the
@@ -62,3 +93,57 @@ generate_fam_struct_impl!(kvm_msr_list, u32, indices, u32, nmsrs, KVM_MAX_MSR_EN
6293/// the array elements, this type is implemented using
6394/// [FamStructWrapper](../vmm_sys_util/fam/struct.FamStructWrapper.html).
6495pub type MsrList = FamStructWrapper < kvm_msr_list > ;
96+
97+ #[ cfg( test) ]
98+ mod tests {
99+ use super :: { CpuId , MsrList , Msrs } ;
100+ use x86:: bindings:: kvm_cpuid_entry2;
101+
102+ #[ test]
103+ fn test_cpuid_eq ( ) {
104+ let entries = & [ kvm_cpuid_entry2:: default ( ) ; 2 ] ;
105+ let mut wrapper = CpuId :: from_entries ( entries) . unwrap ( ) ;
106+ assert_eq ! ( wrapper. as_slice( ) . len( ) , 2 ) ;
107+
108+ let mut wrapper2 = wrapper. clone ( ) ;
109+ assert ! ( wrapper == wrapper2) ;
110+
111+ wrapper. as_mut_slice ( ) [ 1 ] . index = 1 ;
112+ assert ! ( wrapper != wrapper2) ;
113+ wrapper2. as_mut_slice ( ) [ 1 ] . index = 1 ;
114+ assert ! ( wrapper == wrapper2) ;
115+ }
116+ #[ test]
117+ fn test_msrs_eq ( ) {
118+ let mut wrapper = Msrs :: new ( 2 ) . unwrap ( ) ;
119+ assert_eq ! ( wrapper. as_slice( ) . len( ) , 2 ) ;
120+
121+ let mut wrapper2 = wrapper. clone ( ) ;
122+ assert ! ( wrapper == wrapper2) ;
123+
124+ wrapper. as_mut_fam_struct ( ) . pad = 1 ;
125+ assert ! ( wrapper != wrapper2) ;
126+ wrapper2. as_mut_fam_struct ( ) . pad = 1 ;
127+ assert ! ( wrapper == wrapper2) ;
128+
129+ wrapper. as_mut_slice ( ) [ 1 ] . data = 1 ;
130+ assert ! ( wrapper != wrapper2) ;
131+ assert ! ( wrapper. as_slice( ) != wrapper2. as_slice( ) ) ;
132+ wrapper2. as_mut_slice ( ) [ 1 ] . data = 1 ;
133+ assert ! ( wrapper == wrapper2) ;
134+ assert ! ( wrapper. as_slice( ) == wrapper2. as_slice( ) ) ;
135+ }
136+ #[ test]
137+ fn test_msrs_list_eq ( ) {
138+ let mut wrapper = MsrList :: new ( 1 ) . unwrap ( ) ;
139+ assert_eq ! ( wrapper. as_slice( ) . len( ) , 1 ) ;
140+
141+ let mut wrapper2 = wrapper. clone ( ) ;
142+ assert ! ( wrapper == wrapper2) ;
143+
144+ wrapper. as_mut_slice ( ) [ 0 ] = 1 ;
145+ assert ! ( wrapper != wrapper2) ;
146+ wrapper2. as_mut_slice ( ) [ 0 ] = 1 ;
147+ assert ! ( wrapper == wrapper2) ;
148+ }
149+ }
0 commit comments