@@ -82,19 +82,16 @@ impl SnapshotHdr {
82
82
}
83
83
84
84
pub fn load < R : Read > ( reader : & mut R ) -> Result < Self , SnapshotError > {
85
- let hdr: SnapshotHdr = bincode:: DefaultOptions :: new ( )
86
- . with_limit ( VM_STATE_DESERIALIZE_LIMIT )
87
- . with_fixint_encoding ( )
88
- . allow_trailing_bytes ( ) // need this because we deserialize header and snapshot from the same file, so after
89
- // reading the header, there will be trailing bytes.
90
- . deserialize_from ( reader)
91
- . map_err ( |err| SnapshotError :: Serde ( err. to_string ( ) ) ) ?;
85
+ let hdr: SnapshotHdr = deserialize ( reader) ?;
92
86
93
87
Ok ( hdr)
94
88
}
95
89
96
90
pub fn store < W : Write > ( & self , writer : & mut W ) -> Result < ( ) , SnapshotError > {
97
- bincode:: serialize_into ( writer, self ) . map_err ( |err| SnapshotError :: Serde ( err. to_string ( ) ) )
91
+ match serialize ( writer, self ) {
92
+ Ok ( _) => Ok ( ( ) ) ,
93
+ Err ( e) => Err ( e)
94
+ }
98
95
}
99
96
}
100
97
@@ -105,33 +102,17 @@ pub struct Snapshot<Data> {
105
102
pub data : Data ,
106
103
}
107
104
108
- impl < Data : for < ' a > Deserialize < ' a > > Snapshot < Data > {
109
- /// Helper function to deserialize an object from a reader
110
- pub fn deserialize < T , O > ( reader : & mut T ) -> Result < O , SnapshotError >
111
- where
112
- T : Read ,
113
- O : DeserializeOwned + Debug ,
114
- {
115
- // flags below are those used by default by bincode::deserialize_from, plus `with_limit`.
116
- bincode:: DefaultOptions :: new ( )
117
- . with_limit ( VM_STATE_DESERIALIZE_LIMIT )
118
- . with_fixint_encoding ( )
119
- . allow_trailing_bytes ( ) // need this because we deserialize header and snapshot from the same file, so after
120
- // reading the header, there will be trailing bytes.
121
- . deserialize_from ( reader)
122
- . map_err ( |err| SnapshotError :: Serde ( err. to_string ( ) ) )
123
- }
124
-
105
+ impl < Data : DeserializeOwned > Snapshot < Data > {
125
106
pub fn load_unchecked < R : Read > ( reader : & mut R ) -> Result < Self , SnapshotError >
126
107
where
127
108
Data : DeserializeOwned + Debug ,
128
109
{
129
- let hdr: SnapshotHdr = Self :: deserialize ( reader) ?;
110
+ let hdr: SnapshotHdr = deserialize ( reader) ?;
130
111
if hdr. magic != SNAPSHOT_MAGIC_ID {
131
112
return Err ( SnapshotError :: InvalidMagic ( hdr. magic ) ) ;
132
113
}
133
114
134
- let data: Data = Self :: deserialize ( reader) ?;
115
+ let data: Data = deserialize ( reader) ?;
135
116
Ok ( Self { header : hdr, data } )
136
117
}
137
118
@@ -156,7 +137,7 @@ impl<Data: for<'a> Deserialize<'a>> Snapshot<Data> {
156
137
// 2 statements is important, we first get the checksum computed on the read bytes
157
138
// then read the stored checksum.
158
139
let computed_checksum = crc_reader. checksum ( ) ;
159
- let stored_checksum: u64 = Self :: deserialize ( & mut crc_reader) ?;
140
+ let stored_checksum: u64 = deserialize ( & mut crc_reader) ?;
160
141
if computed_checksum != stored_checksum {
161
142
return Err ( SnapshotError :: Crc64 ( computed_checksum) ) ;
162
143
}
@@ -167,29 +148,11 @@ impl<Data: for<'a> Deserialize<'a>> Snapshot<Data> {
167
148
}
168
149
169
150
impl < Data : Serialize + Debug > Snapshot < Data > {
170
- /// Helper function to serialize an object to a writer
171
- pub fn serialize < T , O > ( writer : & mut T , data : & O ) -> Result < usize , SnapshotError >
172
- where
173
- T : Write ,
174
- O : Serialize + Debug ,
175
- {
176
- let mut buffer = Vec :: new ( ) ;
177
- bincode:: serialize_into ( & mut buffer, data)
178
- . map_err ( |err| SnapshotError :: Serde ( err. to_string ( ) ) ) ?;
179
-
180
- writer
181
- . write_all ( & buffer)
182
- . map_err ( |err| SnapshotError :: Serde ( err. to_string ( ) ) ) ?;
183
-
184
- Ok ( buffer. len ( ) )
185
- // bincode::serialize_into(writer, data).map_err(|err| SnapshotError::Serde(err.to_string()))
186
- }
187
-
188
151
pub fn save < W : Write > ( & self , mut writer : & mut W ) -> Result < usize , SnapshotError > {
189
152
// Write magic value and snapshot version
190
- Self :: serialize ( & mut writer, & SnapshotHdr :: new ( self . header . version . clone ( ) ) ) ?;
153
+ serialize ( & mut writer, & SnapshotHdr :: new ( self . header . version . clone ( ) ) ) ?;
191
154
// Write data
192
- Self :: serialize ( & mut writer, & self . data )
155
+ serialize ( & mut writer, & self . data )
193
156
}
194
157
195
158
pub fn save_with_crc < W : Write > ( & self , writer : & mut W ) -> Result < usize , SnapshotError > {
@@ -198,14 +161,52 @@ impl<Data: Serialize + Debug> Snapshot<Data> {
198
161
199
162
// Now write CRC value
200
163
let checksum = crc_writer. checksum ( ) ;
201
- Self :: serialize ( & mut crc_writer, & checksum)
164
+ serialize ( & mut crc_writer, & checksum)
202
165
}
203
166
}
204
167
205
168
impl < Data > Snapshot < Data > {
206
169
pub fn new ( header : SnapshotHdr , data : Data ) -> Self {
207
170
Snapshot { header, data }
208
171
}
172
+
173
+ pub fn version ( & self ) -> Version {
174
+ self . header . version . clone ( )
175
+ }
176
+ }
177
+
178
+ /// Helper function to deserialize an object from a reader
179
+ fn deserialize < T , O > ( reader : & mut T ) -> Result < O , SnapshotError >
180
+ where
181
+ T : Read ,
182
+ O : DeserializeOwned + Debug ,
183
+ {
184
+ // flags below are those used by default by bincode::deserialize_from, plus `with_limit`.
185
+ bincode:: DefaultOptions :: new ( )
186
+ . with_limit ( VM_STATE_DESERIALIZE_LIMIT )
187
+ . with_fixint_encoding ( )
188
+ . allow_trailing_bytes ( ) // need this because we deserialize header and snapshot from the same file, so after
189
+ // reading the header, there will be trailing bytes.
190
+ . deserialize_from ( reader)
191
+ . map_err ( |err| SnapshotError :: Serde ( err. to_string ( ) ) )
192
+ }
193
+
194
+ /// Helper function to serialize an object to a writer
195
+ fn serialize < T , O > ( writer : & mut T , data : & O ) -> Result < usize , SnapshotError >
196
+ where
197
+ T : Write ,
198
+ O : Serialize + Debug ,
199
+ {
200
+ let mut buffer = Vec :: new ( ) ;
201
+ bincode:: serialize_into ( & mut buffer, data)
202
+ . map_err ( |err| SnapshotError :: Serde ( err. to_string ( ) ) ) ?;
203
+
204
+ writer
205
+ . write_all ( & buffer)
206
+ . map_err ( |err| SnapshotError :: Serde ( err. to_string ( ) ) ) ?;
207
+
208
+ Ok ( buffer. len ( ) )
209
+ // bincode::serialize_into(writer, data).map_err(|err| SnapshotError::Serde(err.to_string()))
209
210
}
210
211
211
212
#[ cfg( test) ]
0 commit comments