@@ -3,8 +3,10 @@ mod serde;
3
3
4
4
#[ cfg( feature = "serde" ) ]
5
5
use :: serde:: { Deserialize , Serialize } ;
6
+ use alloc:: boxed:: Box ;
6
7
use alloc:: vec;
7
8
use alloc:: vec:: Vec ;
9
+ use core:: array:: TryFromSliceError ;
8
10
use core:: mem;
9
11
use core:: ops:: { Deref , DerefMut } ;
10
12
use derive_more:: { AsMut , AsRef , Deref , DerefMut } ;
@@ -68,74 +70,55 @@ impl AsMut<[u8]> for RecordWitness {
68
70
/// Internally piece contains a record and corresponding witness that together with records root of
69
71
/// the segment this piece belongs to can be used to verify that a piece belongs to the actual
70
72
/// archival history of the blockchain.
71
- #[ derive( Debug , Clone , PartialEq , Eq , Ord , PartialOrd , Hash , Encode , Decode , TypeInfo ) ]
73
+ #[ derive( Debug , Default , Clone , PartialEq , Eq , Ord , PartialOrd , Hash , Encode , Decode , TypeInfo ) ]
72
74
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
73
- pub struct Piece ( #[ cfg_attr( feature = "serde" , serde( with = "hex::serde" ) ) ] Vec < u8 > ) ;
74
-
75
- impl Default for Piece {
76
- fn default ( ) -> Self {
77
- Self ( vec ! [ 0u8 ; PIECE_SIZE ] )
78
- }
79
- }
75
+ pub struct Piece ( Box < PieceArray > ) ;
80
76
81
77
impl From < Piece > for Vec < u8 > {
82
78
fn from ( piece : Piece ) -> Self {
83
- piece. 0
79
+ piece. 0 . to_vec ( )
84
80
}
85
81
}
86
-
87
82
impl TryFrom < & [ u8 ] > for Piece {
88
- type Error = & ' static str ;
83
+ type Error = TryFromSliceError ;
84
+
89
85
fn try_from ( slice : & [ u8 ] ) -> Result < Self , Self :: Error > {
90
- if slice. len ( ) != PIECE_SIZE {
91
- Err ( "Wrong piece size, expected: 32768" )
92
- } else {
93
- Ok ( Self ( slice. to_vec ( ) ) )
94
- }
86
+ <[ u8 ; PIECE_SIZE ] >:: try_from ( slice) . map ( |bytes| Piece ( Box :: new ( PieceArray ( bytes) ) ) )
95
87
}
96
88
}
97
89
98
90
impl TryFrom < Vec < u8 > > for Piece {
99
- type Error = & ' static str ;
91
+ type Error = TryFromSliceError ;
100
92
101
93
fn try_from ( vec : Vec < u8 > ) -> Result < Self , Self :: Error > {
102
- if vec. len ( ) != PIECE_SIZE {
103
- Err ( "Wrong piece size, expected: 32768" )
104
- } else {
105
- Ok ( Self ( vec) )
106
- }
94
+ // TODO: Maybe possible to transmute boxed slice into boxed array
95
+ Self :: try_from ( vec. as_slice ( ) )
107
96
}
108
97
}
109
98
110
99
impl Deref for Piece {
111
100
type Target = PieceArray ;
112
101
113
102
fn deref ( & self ) -> & Self :: Target {
114
- let array =
115
- <& [ u8 ; PIECE_SIZE ] >:: try_from ( self . 0 . as_slice ( ) ) . expect ( "Piece has correct size; qed" ) ;
116
- // SAFETY: Same memory layout due to `#[repr(transparent)]`
117
- unsafe { mem:: transmute ( array) }
103
+ & self . 0
118
104
}
119
105
}
120
106
121
107
impl DerefMut for Piece {
122
108
fn deref_mut ( & mut self ) -> & mut Self :: Target {
123
- let array = <& mut [ u8 ; PIECE_SIZE ] >:: try_from ( self . 0 . as_mut_slice ( ) )
124
- . expect ( "Piece has correct size; qed" ) ;
125
- // SAFETY: Same memory layout due to `#[repr(transparent)]`
126
- unsafe { mem:: transmute ( array) }
109
+ & mut self . 0
127
110
}
128
111
}
129
112
130
113
impl AsRef < [ u8 ] > for Piece {
131
114
fn as_ref ( & self ) -> & [ u8 ] {
132
- & self . 0
115
+ self . 0 . as_slice ( )
133
116
}
134
117
}
135
118
136
119
impl AsMut < [ u8 ] > for Piece {
137
120
fn as_mut ( & mut self ) -> & mut [ u8 ] {
138
- & mut self . 0
121
+ self . 0 . as_mut_slice ( )
139
122
}
140
123
}
141
124
@@ -187,13 +170,13 @@ impl AsMut<[u8]> for PieceArray {
187
170
188
171
impl From < & PieceArray > for Piece {
189
172
fn from ( value : & PieceArray ) -> Self {
190
- Piece ( value . 0 . to_vec ( ) )
173
+ Piece ( Box :: new ( * value ) )
191
174
}
192
175
}
193
176
194
177
impl From < PieceArray > for Piece {
195
178
fn from ( value : PieceArray ) -> Self {
196
- Piece ( value . 0 . to_vec ( ) )
179
+ Piece ( Box :: new ( value ) )
197
180
}
198
181
}
199
182
0 commit comments