|
14 | 14 | #![allow(clippy::unreadable_literal)]
|
15 | 15 | #![deny(private_in_public)]
|
16 | 16 |
|
17 |
| -use std::{fmt, io}; |
18 |
| -use std::any::Any; |
19 |
| -use std::cmp::Ordering; |
20 |
| -use std::convert::TryInto; |
21 |
| -use std::fmt::{Display, Formatter}; |
22 |
| -use std::hash::Hash; |
23 |
| - |
24 | 17 | use failure::Fail;
|
| 18 | +use std::{any::Any, fmt, hash::Hash, io::{self, Read, Write}}; |
25 | 19 |
|
26 |
| -use ton_block::ShardIdent; |
27 |
| -pub use ton_types::Result; |
| 20 | +use ton_block::{BlockIdExt, ShardIdent}; |
| 21 | +use ton_types::Result; |
28 | 22 | use ton_types::UInt256;
|
29 | 23 |
|
30 |
| -use crate::ton::ton_node::blockidext::BlockIdExt; |
31 |
| - |
32 | 24 | macro_rules! _invalid_id {
|
33 | 25 | ($id:ident) => {
|
34 | 26 | Err(crate::InvalidConstructor { expected: Self::possible_constructors(), received: $id }.into())
|
@@ -60,12 +52,12 @@ pub struct InvalidConstructor {
|
60 | 52 |
|
61 | 53 | /// Struct for deserializing TL-scheme objects from any `io::Read`
|
62 | 54 | pub struct Deserializer<'r> {
|
63 |
| - reader: &'r mut dyn io::Read, |
| 55 | + reader: &'r mut dyn Read, |
64 | 56 | }
|
65 | 57 |
|
66 | 58 | impl<'r> Deserializer<'r> {
|
67 | 59 | /// Create `Deserializer` with given `io::Read` trait object
|
68 |
| - pub fn new(reader: &'r mut dyn io::Read) -> Self { |
| 60 | + pub fn new(reader: &'r mut dyn Read) -> Self { |
69 | 61 | Deserializer { reader }
|
70 | 62 | }
|
71 | 63 |
|
@@ -95,7 +87,7 @@ impl<'r> Deserializer<'r> {
|
95 | 87 | }
|
96 | 88 | }
|
97 | 89 |
|
98 |
| -impl<'r> io::Read for Deserializer<'r> { |
| 90 | +impl<'r> Read for Deserializer<'r> { |
99 | 91 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
100 | 92 | self.reader.read(buf)
|
101 | 93 | }
|
@@ -165,12 +157,12 @@ impl DynamicDeserializer {
|
165 | 157 |
|
166 | 158 | /// Struct for serializing TL-scheme objects into any `io::Write`
|
167 | 159 | pub struct Serializer<'w> {
|
168 |
| - writer: &'w mut dyn io::Write, |
| 160 | + writer: &'w mut dyn Write, |
169 | 161 | }
|
170 | 162 |
|
171 | 163 | impl<'w> Serializer<'w> {
|
172 | 164 | /// Create `Serializer` with given `io::Write` trait object
|
173 |
| - pub fn new(writer: &'w mut dyn io::Write) -> Self { |
| 165 | + pub fn new(writer: &'w mut dyn Write) -> Self { |
174 | 166 | Serializer { writer }
|
175 | 167 | }
|
176 | 168 |
|
@@ -205,7 +197,7 @@ impl<'w> Serializer<'w> {
|
205 | 197 | }
|
206 | 198 | }
|
207 | 199 |
|
208 |
| -impl<'w> io::Write for Serializer<'w> { |
| 200 | +impl<'w> Write for Serializer<'w> { |
209 | 201 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
210 | 202 | self.writer.write(buf)
|
211 | 203 | }
|
@@ -266,85 +258,70 @@ pub trait Function: AnyBoxedSerialize {
|
266 | 258 | type Reply: BoxedDeserialize + AnyBoxedSerialize;
|
267 | 259 | }
|
268 | 260 |
|
269 |
| -impl PartialOrd for BlockIdExt { |
270 |
| - fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
271 |
| - Some(self.cmp(other)) |
272 |
| - } |
273 |
| -} |
274 |
| - |
275 |
| -impl Ord for BlockIdExt { |
276 |
| - fn cmp(&self, other: &Self) -> Ordering { |
277 |
| - self.workchain.cmp(&other.workchain) |
278 |
| - .then(self.shard.cmp(&other.shard)) |
279 |
| - .then(self.seqno.cmp(&other.seqno)) |
280 |
| - .then(self.root_hash.cmp(&other.root_hash)) |
281 |
| - .then(self.file_hash.cmp(&other.file_hash)) |
| 261 | +impl BareDeserialize for BlockIdExt { |
| 262 | + fn deserialize_bare(de: &mut Deserializer) -> Result<Self> { |
| 263 | + let shard = ShardIdent::with_tagged_prefix( |
| 264 | + de.read_bare::<crate::ton::int>()?, |
| 265 | + de.read_bare::<crate::ton::long>()? as u64 |
| 266 | + )?; |
| 267 | + let ret = Self::with_params( |
| 268 | + shard, |
| 269 | + de.read_bare::<crate::ton::int>()? as u32, |
| 270 | + de.read_bare::<UInt256>()?, |
| 271 | + de.read_bare::<UInt256>()? |
| 272 | + ); |
| 273 | + Ok(ret) |
282 | 274 | }
|
283 | 275 | }
|
284 | 276 |
|
285 |
| -impl Display for BlockIdExt { |
286 |
| - fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
287 |
| - write!(f, "({}:{:016x}, {}, rh {}, fh {})", self.workchain, self.shard, self.seqno, hex::encode(self.root_hash.0), hex::encode(self.file_hash.0)) |
| 277 | +impl BareSerialize for BlockIdExt { |
| 278 | + fn constructor(&self) -> ConstructorNumber { |
| 279 | + crate::ton::ton_node::blockidext::TL_TAG |
288 | 280 | }
|
289 |
| -} |
290 |
| - |
291 |
| -impl From<&ton_block::BlockIdExt> for BlockIdExt { |
292 |
| - fn from(block_id_ext: &ton_block::BlockIdExt) -> Self { |
293 |
| - Self { |
294 |
| - workchain: block_id_ext.shard().workchain_id(), |
295 |
| - shard: block_id_ext.shard().shard_prefix_with_tag() as i64, |
296 |
| - seqno: block_id_ext.seq_no() as i32, |
297 |
| - root_hash: block_id_ext.root_hash().into(), |
298 |
| - file_hash: block_id_ext.file_hash().into(), |
299 |
| - } |
300 |
| - } |
301 |
| -} |
302 |
| - |
303 |
| -impl From<ton_block::BlockIdExt> for BlockIdExt { |
304 |
| - fn from(block_id_ext: ton_block::BlockIdExt) -> Self { |
305 |
| - Self::from(&block_id_ext) |
| 281 | + fn serialize_bare(&self, se: &mut Serializer) -> Result<()> { |
| 282 | + let shard = self.shard(); |
| 283 | + se.write_bare::<crate::ton::int>(&shard.workchain_id())?; |
| 284 | + se.write_bare::<crate::ton::long>(&(shard.shard_prefix_with_tag() as i64))?; |
| 285 | + se.write_bare::<crate::ton::int>(&(self.seq_no() as i32))?; |
| 286 | + se.write_bare::<UInt256>(self.root_hash())?; |
| 287 | + se.write_bare::<UInt256>(self.file_hash())?; |
| 288 | + Ok(()) |
306 | 289 | }
|
307 | 290 | }
|
308 | 291 |
|
309 |
| -impl TryInto<ton_block::BlockIdExt> for &BlockIdExt { |
310 |
| - type Error = failure::Error; |
311 |
| - |
312 |
| - fn try_into(self) -> Result<ton_block::BlockIdExt> { |
313 |
| - Ok(ton_block::BlockIdExt::with_params( |
314 |
| - ShardIdent::with_tagged_prefix(self.workchain, self.shard as u64)?, |
315 |
| - self.seqno as u32, |
316 |
| - self.root_hash.into(), |
317 |
| - self.file_hash.into(), |
318 |
| - )) |
| 292 | +impl BoxedDeserialize for BlockIdExt { |
| 293 | + fn possible_constructors() -> Vec<crate::ConstructorNumber> { |
| 294 | + vec![crate::ton::ton_node::blockidext::TL_TAG] |
319 | 295 | }
|
320 |
| -} |
321 |
| - |
322 |
| -impl Display for crate::ton::int256 { |
323 |
| - fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
324 |
| - write!(f, "{}", hex::encode(self.0)) |
| 296 | + fn deserialize_boxed(id: ConstructorNumber, de: &mut Deserializer) -> Result<Self> { |
| 297 | + if id == crate::ton::ton_node::blockidext::TL_TAG { |
| 298 | + de.read_bare() |
| 299 | + } else { |
| 300 | + _invalid_id!(id) |
| 301 | + } |
325 | 302 | }
|
326 | 303 | }
|
327 | 304 |
|
328 |
| -impl From<UInt256> for crate::ton::int256 { |
329 |
| - fn from(value: UInt256) -> Self { |
330 |
| - Self(value.into()) |
| 305 | +impl BoxedSerialize for BlockIdExt { |
| 306 | + fn serialize_boxed(&self) -> (ConstructorNumber, &dyn BareSerialize) { |
| 307 | + (crate::ton::ton_node::blockidext::TL_TAG, self) |
331 | 308 | }
|
332 | 309 | }
|
333 | 310 |
|
334 |
| -impl From<&UInt256> for crate::ton::int256 { |
335 |
| - fn from(value: &UInt256) -> Self { |
336 |
| - Self(value.clone().into()) |
| 311 | +impl BareDeserialize for UInt256 { |
| 312 | + fn deserialize_bare(de: &mut Deserializer) -> Result<Self> { |
| 313 | + let mut data = [0u8; 32]; |
| 314 | + de.read_exact(&mut data)?; |
| 315 | + Ok(Self::with_array(data)) |
337 | 316 | }
|
338 | 317 | }
|
339 | 318 |
|
340 |
| -impl Into<UInt256> for crate::ton::int256 { |
341 |
| - fn into(self) -> UInt256 { |
342 |
| - UInt256::from(self.0) |
| 319 | +impl BareSerialize for UInt256 { |
| 320 | + fn constructor(&self) -> ConstructorNumber { |
| 321 | + unreachable!() |
343 | 322 | }
|
344 |
| -} |
345 |
| - |
346 |
| -impl Into<UInt256> for &crate::ton::int256 { |
347 |
| - fn into(self) -> UInt256 { |
348 |
| - UInt256::from(self.0) |
| 323 | + fn serialize_bare(&self, se: &mut Serializer) -> Result<()> { |
| 324 | + se.write_all(self.as_slice())?; |
| 325 | + Ok(()) |
349 | 326 | }
|
350 | 327 | }
|
0 commit comments