-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathcodec.rs
2086 lines (1773 loc) · 57.4 KB
/
codec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017-2018 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Serialization.
use core::fmt;
use core::{
convert::TryFrom,
iter::FromIterator,
marker::PhantomData,
mem,
mem::{
MaybeUninit,
},
ops::{Deref, Range, RangeInclusive},
time::Duration,
};
use core::num::{
NonZeroI8,
NonZeroI16,
NonZeroI32,
NonZeroI64,
NonZeroI128,
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128,
};
use byte_slice_cast::{AsByteSlice, AsMutByteSlice, ToMutByteSlice};
#[cfg(target_has_atomic = "ptr")]
use crate::alloc::sync::Arc;
use crate::alloc::{
boxed::Box,
borrow::{Cow, ToOwned},
collections::{
BTreeMap, BTreeSet, VecDeque, LinkedList, BinaryHeap
},
rc::Rc,
string::String,
vec::Vec,
};
use crate::compact::Compact;
use crate::DecodeFinished;
use crate::encode_like::EncodeLike;
use crate::Error;
pub(crate) const MAX_PREALLOCATION: usize = 4 * 1024;
const A_BILLION: u32 = 1_000_000_000;
/// Trait that allows reading of data into a slice.
pub trait Input {
/// Should return the remaining length of the input data. If no information about the input
/// length is available, `None` should be returned.
///
/// The length is used to constrain the preallocation while decoding. Returning a garbage
/// length can open the doors for a denial of service attack to your application.
/// Otherwise, returning `None` can decrease the performance of your application.
fn remaining_len(&mut self) -> Result<Option<usize>, Error>;
/// Read the exact number of bytes required to fill the given buffer.
///
/// Note that this function is similar to `std::io::Read::read_exact` and not
/// `std::io::Read::read`.
fn read(&mut self, into: &mut [u8]) -> Result<(), Error>;
/// Read a single byte from the input.
fn read_byte(&mut self) -> Result<u8, Error> {
let mut buf = [0u8];
self.read(&mut buf[..])?;
Ok(buf[0])
}
/// Descend into nested reference when decoding.
/// This is called when decoding a new refence-based instance,
/// such as `Vec` or `Box`. Currently all such types are
/// allocated on the heap.
fn descend_ref(&mut self) -> Result<(), Error> {
Ok(())
}
/// Ascend to previous structure level when decoding.
/// This is called when decoding reference-based type is finished.
fn ascend_ref(&mut self) {}
/// !INTERNAL USE ONLY!
///
/// Decodes a `bytes::Bytes`.
#[cfg(feature = "bytes")]
#[doc(hidden)]
fn scale_internal_decode_bytes(&mut self) -> Result<bytes::Bytes, Error> where Self: Sized {
Vec::<u8>::decode(self).map(bytes::Bytes::from)
}
}
impl<'a> Input for &'a [u8] {
fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
Ok(Some(self.len()))
}
fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
if into.len() > self.len() {
return Err("Not enough data to fill buffer".into());
}
let len = into.len();
into.copy_from_slice(&self[..len]);
*self = &self[len..];
Ok(())
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
use std::io::ErrorKind::*;
match err.kind() {
NotFound => "io error: NotFound".into(),
PermissionDenied => "io error: PermissionDenied".into(),
ConnectionRefused => "io error: ConnectionRefused".into(),
ConnectionReset => "io error: ConnectionReset".into(),
ConnectionAborted => "io error: ConnectionAborted".into(),
NotConnected => "io error: NotConnected".into(),
AddrInUse => "io error: AddrInUse".into(),
AddrNotAvailable => "io error: AddrNotAvailable".into(),
BrokenPipe => "io error: BrokenPipe".into(),
AlreadyExists => "io error: AlreadyExists".into(),
WouldBlock => "io error: WouldBlock".into(),
InvalidInput => "io error: InvalidInput".into(),
InvalidData => "io error: InvalidData".into(),
TimedOut => "io error: TimedOut".into(),
WriteZero => "io error: WriteZero".into(),
Interrupted => "io error: Interrupted".into(),
Other => "io error: Other".into(),
UnexpectedEof => "io error: UnexpectedEof".into(),
_ => "io error: Unknown".into(),
}
}
}
/// Wrapper that implements Input for any `Read` type.
#[cfg(feature = "std")]
pub struct IoReader<R: std::io::Read>(pub R);
#[cfg(feature = "std")]
impl<R: std::io::Read> Input for IoReader<R> {
fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
Ok(None)
}
fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
self.0.read_exact(into).map_err(Into::into)
}
}
/// Trait that allows writing of data.
pub trait Output {
/// Write to the output.
fn write(&mut self, bytes: &[u8]);
/// Write a single byte to the output.
fn push_byte(&mut self, byte: u8) {
self.write(&[byte]);
}
}
#[cfg(not(feature = "std"))]
impl Output for Vec<u8> {
fn write(&mut self, bytes: &[u8]) {
self.extend_from_slice(bytes)
}
}
#[cfg(feature = "std")]
impl<W: std::io::Write> Output for W {
fn write(&mut self, bytes: &[u8]) {
(self as &mut dyn std::io::Write).write_all(bytes).expect("Codec outputs are infallible");
}
}
/// !INTERNAL USE ONLY!
///
/// This enum provides type information to optimize encoding/decoding by doing fake specialization.
#[doc(hidden)]
#[non_exhaustive]
pub enum TypeInfo {
/// Default value of [`Encode::TYPE_INFO`] to not require implementors to set this value in the trait.
Unknown,
U8,
I8,
U16,
I16,
U32,
I32,
U64,
I64,
U128,
I128,
F32,
F64,
}
/// Trait that allows zero-copy write of value-references to slices in LE format.
///
/// Implementations should override `using_encoded` for value types and `encode_to` and `size_hint` for allocating types.
/// Wrapper types should override all methods.
pub trait Encode {
// !INTERNAL USE ONLY!
// This const helps SCALE to optimize the encoding/decoding by doing fake specialization.
#[doc(hidden)]
const TYPE_INFO: TypeInfo = TypeInfo::Unknown;
/// If possible give a hint of expected size of the encoding.
///
/// This method is used inside default implementation of `encode`
/// to avoid re-allocations.
fn size_hint(&self) -> usize {
0
}
/// Convert self to a slice and append it to the destination.
fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
self.using_encoded(|buf| dest.write(buf));
}
/// Convert self to an owned vector.
fn encode(&self) -> Vec<u8> {
let mut r = Vec::with_capacity(self.size_hint());
self.encode_to(&mut r);
r
}
/// Convert self to a slice and then invoke the given closure with it.
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.encode())
}
/// Calculates the encoded size.
///
/// Should be used when the encoded data isn't required.
///
/// # Note
///
/// This works by using a special [`Output`] that only tracks the size. So, there are no allocations inside the
/// output. However, this can not prevent allocations that some types are doing inside their own encoding.
fn encoded_size(&self) -> usize {
let mut size_tracker = SizeTracker { written: 0 };
self.encode_to(&mut size_tracker);
size_tracker.written
}
}
// Implements `Output` and only keeps track of the number of written bytes
struct SizeTracker {
written: usize,
}
impl Output for SizeTracker {
fn write(&mut self, bytes: &[u8]) {
self.written += bytes.len();
}
fn push_byte(&mut self, _byte: u8) {
self.written += 1;
}
}
/// Trait that allows the length of a collection to be read, without having
/// to read and decode the entire elements.
pub trait DecodeLength {
/// Return the number of elements in `self_encoded`.
fn len(self_encoded: &[u8]) -> Result<usize, Error>;
}
/// Trait that allows zero-copy read of value-references from slices in LE format.
pub trait Decode: Sized {
// !INTERNAL USE ONLY!
// This const helps SCALE to optimize the encoding/decoding by doing fake specialization.
#[doc(hidden)]
const TYPE_INFO: TypeInfo = TypeInfo::Unknown;
/// Attempt to deserialise the value from input.
fn decode<I: Input>(input: &mut I) -> Result<Self, Error>;
/// Attempt to deserialize the value from input into a pre-allocated piece of memory.
///
/// The default implementation will just call [`Decode::decode`].
///
/// # Safety
///
/// If this function returns `Ok` then `dst` **must** be properly initialized.
///
/// This is enforced by requiring the implementation to return a [`DecodeFinished`]
/// which can only be created by calling [`DecodeFinished::assert_decoding_finished`] which is `unsafe`.
fn decode_into<I: Input>(input: &mut I, dst: &mut MaybeUninit<Self>) -> Result<DecodeFinished, Error> {
let value = Self::decode(input)?;
dst.write(value);
// SAFETY: We've written the decoded value to `dst` so calling this is safe.
unsafe { Ok(DecodeFinished::assert_decoding_finished()) }
}
/// Attempt to skip the encoded value from input.
///
/// The default implementation of this function is just calling [`Decode::decode`].
/// When possible, an implementation should provide a specialized implementation.
fn skip<I: Input>(input: &mut I) -> Result<(), Error> {
Self::decode(input).map(|_| ())
}
/// Returns the fixed encoded size of the type.
///
/// If it returns `Some(size)` then all possible values of this
/// type have the given size (in bytes) when encoded.
///
/// NOTE: A type with a fixed encoded size may return `None`.
fn encoded_fixed_size() -> Option<usize> {
None
}
}
/// Trait that allows zero-copy read/write of value-references to/from slices in LE format.
pub trait Codec: Decode + Encode {}
impl<S: Decode + Encode> Codec for S {}
/// Trait that bound `EncodeLike` along with `Encode`. Usefull for generic being used in function
/// with `EncodeLike` parameters.
pub trait FullEncode: Encode + EncodeLike {}
impl<S: Encode + EncodeLike> FullEncode for S {}
/// Trait that bound `EncodeLike` along with `Codec`. Usefull for generic being used in function
/// with `EncodeLike` parameters.
pub trait FullCodec: Decode + FullEncode {}
impl<S: Decode + FullEncode> FullCodec for S {}
/// A marker trait for types that wrap other encodable type.
///
/// Such types should not carry any additional information
/// that would require to be encoded, because the encoding
/// is assumed to be the same as the wrapped type.
///
/// The wrapped type that is referred to is the [`Deref::Target`].
pub trait WrapperTypeEncode: Deref {}
impl<T: ?Sized> WrapperTypeEncode for Box<T> {}
impl<T: ?Sized + Encode> EncodeLike for Box<T> {}
impl<T: Encode> EncodeLike<T> for Box<T> {}
impl<T: Encode> EncodeLike<Box<T>> for T {}
impl<T: ?Sized> WrapperTypeEncode for &T {}
impl<T: ?Sized + Encode> EncodeLike for &T {}
impl<T: Encode> EncodeLike<T> for &T {}
impl<T: Encode> EncodeLike<&T> for T {}
impl<T: Encode> EncodeLike<T> for &&T {}
impl<T: Encode> EncodeLike<&&T> for T {}
impl<T: ?Sized> WrapperTypeEncode for &mut T {}
impl<T: ?Sized + Encode> EncodeLike for &mut T {}
impl<T: Encode> EncodeLike<T> for &mut T {}
impl<T: Encode> EncodeLike<&mut T> for T {}
impl<'a, T: ToOwned + ?Sized> WrapperTypeEncode for Cow<'a, T> {}
impl<'a, T: ToOwned + Encode + ?Sized> EncodeLike for Cow<'a, T> {}
impl<'a, T: ToOwned + Encode> EncodeLike<T> for Cow<'a, T> {}
impl<'a, T: ToOwned + Encode> EncodeLike<Cow<'a, T>> for T {}
impl<T: ?Sized> WrapperTypeEncode for Rc<T> {}
impl<T: ?Sized + Encode> EncodeLike for Rc<T> {}
impl<T: Encode> EncodeLike<T> for Rc<T> {}
impl<T: Encode> EncodeLike<Rc<T>> for T {}
impl WrapperTypeEncode for String {}
impl EncodeLike for String {}
impl EncodeLike<&str> for String {}
impl EncodeLike<String> for &str {}
#[cfg(target_has_atomic = "ptr")]
mod atomic_ptr_targets {
use super::*;
impl<T: ?Sized> WrapperTypeEncode for Arc<T> {}
impl<T: ?Sized + Encode> EncodeLike for Arc<T> {}
impl<T: Encode> EncodeLike<T> for Arc<T> {}
impl<T: Encode> EncodeLike<Arc<T>> for T {}
}
#[cfg(feature = "bytes")]
mod feature_wrapper_bytes {
use super::*;
use bytes::Bytes;
impl WrapperTypeEncode for Bytes {}
impl EncodeLike for Bytes {}
impl EncodeLike<&[u8]> for Bytes {}
impl EncodeLike<Vec<u8>> for Bytes {}
impl EncodeLike<Bytes> for &[u8] {}
impl EncodeLike<Bytes> for Vec<u8> {}
}
#[cfg(feature = "bytes")]
struct BytesCursor {
bytes: bytes::Bytes,
position: usize
}
#[cfg(feature = "bytes")]
impl Input for BytesCursor {
fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
Ok(Some(self.bytes.len() - self.position))
}
fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
if into.len() > self.bytes.len() - self.position {
return Err("Not enough data to fill buffer".into())
}
into.copy_from_slice(&self.bytes[self.position..self.position + into.len()]);
self.position += into.len();
Ok(())
}
fn scale_internal_decode_bytes(&mut self) -> Result<bytes::Bytes, Error> {
let length = <Compact<u32>>::decode(self)?.0 as usize;
bytes::Buf::advance(&mut self.bytes, self.position);
self.position = 0;
if length > self.bytes.len() {
return Err("Not enough data to fill buffer".into());
}
Ok(self.bytes.split_to(length))
}
}
/// Decodes a given `T` from `Bytes`.
#[cfg(feature = "bytes")]
pub fn decode_from_bytes<T>(bytes: bytes::Bytes) -> Result<T, Error> where T: Decode {
// We could just use implement `Input` for `Bytes` and use `Bytes::split_to`
// to move the cursor, however doing it this way allows us to prevent an
// unnecessary allocation when the `T` which is being deserialized doesn't
// take advantage of the fact that it's being deserialized from `Bytes`.
//
// `Bytes` can be cheaply created from a `Vec<u8>`. It is both zero-copy
// *and* zero-allocation. However once you `.clone()` it or call `split_to()`
// an extra one-time allocation is triggered where the `Bytes` changes it's internal
// representation from essentially being a `Box<[u8]>` into being an `Arc<Box<[u8]>>`.
//
// If the `T` is `Bytes` or is a structure which contains `Bytes` in it then
// we don't really care, because this allocation will have to be made anyway.
//
// However, if `T` doesn't contain any `Bytes` then this extra allocation is
// technically unnecessary, and we can avoid it by tracking the position ourselves
// and treating the underlying `Bytes` as a fancy `&[u8]`.
let mut input = BytesCursor {
bytes,
position: 0
};
T::decode(&mut input)
}
#[cfg(feature = "bytes")]
impl Decode for bytes::Bytes {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
input.scale_internal_decode_bytes()
}
}
impl<T, X> Encode for X where
T: Encode + ?Sized,
X: WrapperTypeEncode<Target = T>,
{
fn size_hint(&self) -> usize {
(**self).size_hint()
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
(**self).using_encoded(f)
}
fn encode(&self) -> Vec<u8> {
(**self).encode()
}
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
(**self).encode_to(dest)
}
}
/// A marker trait for types that can be created solely from other decodable types.
///
/// The decoding of such type is assumed to be the same as the wrapped type.
pub trait WrapperTypeDecode: Sized {
/// A wrapped type.
type Wrapped: Into<Self>;
// !INTERNAL USE ONLY!
// This is a used to specialize `decode` for the wrapped type.
#[doc(hidden)]
#[inline]
fn decode_wrapped<I: Input>(input: &mut I) -> Result<Self, Error> where Self::Wrapped: Decode {
input.descend_ref()?;
let result = Ok(Self::Wrapped::decode(input)?.into());
input.ascend_ref();
result
}
}
impl<T> WrapperTypeDecode for Box<T> {
type Wrapped = T;
fn decode_wrapped<I: Input>(input: &mut I) -> Result<Self, Error> where Self::Wrapped: Decode {
input.descend_ref()?;
// Placement new is not yet stable, but we can just manually allocate a chunk of memory
// and convert it to a `Box` ourselves.
//
// The explicit types here are written out for clarity.
//
// TODO: Use `Box::new_uninit` once that's stable.
let layout = core::alloc::Layout::new::<MaybeUninit<T>>();
let ptr: *mut MaybeUninit<T> = if layout.size() == 0 {
core::ptr::NonNull::dangling().as_ptr()
} else {
// SAFETY: Layout has a non-zero size so calling this is safe.
let ptr: *mut u8 = unsafe {
crate::alloc::alloc::alloc(layout)
};
if ptr.is_null() {
crate::alloc::alloc::handle_alloc_error(layout);
}
ptr.cast()
};
// SAFETY: Constructing a `Box` from a piece of memory allocated with `std::alloc::alloc`
// is explicitly allowed as long as it was allocated with the global allocator
// and the memory layout matches.
//
// Constructing a `Box` from `NonNull::dangling` is also always safe as long
// as the underlying type is zero-sized.
let mut boxed: Box<MaybeUninit<T>> = unsafe { Box::from_raw(ptr) };
T::decode_into(input, &mut boxed)?;
// Decoding succeeded, so let's get rid of `MaybeUninit`.
//
// TODO: Use `Box::assume_init` once that's stable.
let ptr: *mut MaybeUninit<T> = Box::into_raw(boxed);
let ptr: *mut T = ptr.cast();
// SAFETY: `MaybeUninit` doesn't affect the memory layout, so casting the pointer back
// into a `Box` is safe.
let boxed: Box<T> = unsafe { Box::from_raw(ptr) };
input.ascend_ref();
Ok(boxed)
}
}
impl<T> WrapperTypeDecode for Rc<T> {
type Wrapped = T;
fn decode_wrapped<I: Input>(input: &mut I) -> Result<Self, Error> where Self::Wrapped: Decode {
// TODO: This is inefficient; use `Rc::new_uninit` once that's stable.
Box::<T>::decode(input).map(|output| output.into())
}
}
#[cfg(target_has_atomic = "ptr")]
impl<T> WrapperTypeDecode for Arc<T> {
type Wrapped = T;
fn decode_wrapped<I: Input>(input: &mut I) -> Result<Self, Error> where Self::Wrapped: Decode {
// TODO: This is inefficient; use `Arc::new_uninit` once that's stable.
Box::<T>::decode(input).map(|output| output.into())
}
}
impl<T, X> Decode for X where
T: Decode + Into<X>,
X: WrapperTypeDecode<Wrapped=T>,
{
#[inline]
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Self::decode_wrapped(input)
}
}
/// A macro that matches on a [`TypeInfo`] and expands a given macro per variant.
///
/// The first parameter to the given macro will be the type of variant (e.g. `u8`, `u32`, etc.) and other parameters
/// given to this macro.
///
/// The last parameter is the code that should be executed for the `Unknown` type info.
macro_rules! with_type_info {
( $type_info:expr, $macro:ident $( ( $( $params:ident ),* ) )?, { $( $unknown_variant:tt )* }, ) => {
match $type_info {
TypeInfo::U8 => { $macro!(u8 $( $( , $params )* )? ) },
TypeInfo::I8 => { $macro!(i8 $( $( , $params )* )? ) },
TypeInfo::U16 => { $macro!(u16 $( $( , $params )* )? ) },
TypeInfo::I16 => { $macro!(i16 $( $( , $params )* )? ) },
TypeInfo::U32 => { $macro!(u32 $( $( , $params )* )? ) },
TypeInfo::I32 => { $macro!(i32 $( $( , $params )* )? ) },
TypeInfo::U64 => { $macro!(u64 $( $( , $params )* )? ) },
TypeInfo::I64 => { $macro!(i64 $( $( , $params )* )? ) },
TypeInfo::U128 => { $macro!(u128 $( $( , $params )* )? ) },
TypeInfo::I128 => { $macro!(i128 $( $( , $params )* )? ) },
TypeInfo::Unknown => { $( $unknown_variant )* },
TypeInfo::F32 => { $macro!(f32 $( $( , $params )* )? ) },
TypeInfo::F64 => { $macro!(f64 $( $( , $params )* )? ) },
}
};
}
/// Something that can be encoded as a reference.
pub trait EncodeAsRef<'a, T: 'a> {
/// The reference type that is used for encoding.
type RefType: Encode + From<&'a T>;
}
impl<T: Encode, E: Encode> Encode for Result<T, E> {
fn size_hint(&self) -> usize {
1 + match *self {
Ok(ref t) => t.size_hint(),
Err(ref t) => t.size_hint(),
}
}
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
match *self {
Ok(ref t) => {
dest.push_byte(0);
t.encode_to(dest);
}
Err(ref e) => {
dest.push_byte(1);
e.encode_to(dest);
}
}
}
}
impl<T, LikeT, E, LikeE> EncodeLike<Result<LikeT, LikeE>> for Result<T, E>
where
T: EncodeLike<LikeT>,
LikeT: Encode,
E: EncodeLike<LikeE>,
LikeE: Encode,
{}
impl<T: Decode, E: Decode> Decode for Result<T, E> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
match input.read_byte()
.map_err(|e| e.chain("Could not result variant byte for `Result`"))?
{
0 => Ok(
Ok(T::decode(input).map_err(|e| e.chain("Could not Decode `Result::Ok(T)`"))?)
),
1 => Ok(
Err(E::decode(input).map_err(|e| e.chain("Could not decode `Result::Error(E)`"))?)
),
_ => Err("unexpected first byte decoding Result".into()),
}
}
}
/// Shim type because we can't do a specialised implementation for `Option<bool>` directly.
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct OptionBool(pub Option<bool>);
impl fmt::Debug for OptionBool {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl Encode for OptionBool {
fn size_hint(&self) -> usize {
1
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&[match *self {
OptionBool(None) => 0u8,
OptionBool(Some(true)) => 1u8,
OptionBool(Some(false)) => 2u8,
}])
}
}
impl EncodeLike for OptionBool {}
impl Decode for OptionBool {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
match input.read_byte()? {
0 => Ok(OptionBool(None)),
1 => Ok(OptionBool(Some(true))),
2 => Ok(OptionBool(Some(false))),
_ => Err("unexpected first byte decoding OptionBool".into()),
}
}
}
impl<T: EncodeLike<U>, U: Encode> EncodeLike<Option<U>> for Option<T> {}
impl<T: Encode> Encode for Option<T> {
fn size_hint(&self) -> usize {
1 + match *self {
Some(ref t) => t.size_hint(),
None => 0,
}
}
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
match *self {
Some(ref t) => {
dest.push_byte(1);
t.encode_to(dest);
}
None => dest.push_byte(0),
}
}
}
impl<T: Decode> Decode for Option<T> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
match input.read_byte()
.map_err(|e| e.chain("Could not decode variant byte for `Option`"))?
{
0 => Ok(None),
1 => Ok(
Some(T::decode(input).map_err(|e| e.chain("Could not decode `Option::Some(T)`"))?)
),
_ => Err("unexpected first byte decoding Option".into()),
}
}
}
macro_rules! impl_for_non_zero {
( $( $name:ty ),* $(,)? ) => {
$(
impl Encode for $name {
fn size_hint(&self) -> usize {
self.get().size_hint()
}
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
self.get().encode_to(dest)
}
fn encode(&self) -> Vec<u8> {
self.get().encode()
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.get().using_encoded(f)
}
}
impl EncodeLike for $name {}
impl Decode for $name {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Self::new(Decode::decode(input)?)
.ok_or_else(|| Error::from("cannot create non-zero number from 0"))
}
}
)*
}
}
/// Encode the slice without prepending the len.
///
/// This is equivalent to encoding all the element one by one, but it is optimized for some types.
pub(crate) fn encode_slice_no_len<T: Encode, W: Output + ?Sized>(slice: &[T], dest: &mut W) {
macro_rules! encode_to {
( u8, $slice:ident, $dest:ident ) => {{
let typed = unsafe { mem::transmute::<&[T], &[u8]>(&$slice[..]) };
$dest.write(&typed)
}};
( i8, $slice:ident, $dest:ident ) => {{
// `i8` has the same size as `u8`. We can just convert it here and write to the
// dest buffer directly.
let typed = unsafe { mem::transmute::<&[T], &[u8]>(&$slice[..]) };
$dest.write(&typed)
}};
( $ty:ty, $slice:ident, $dest:ident ) => {{
if cfg!(target_endian = "little") {
let typed = unsafe { mem::transmute::<&[T], &[$ty]>(&$slice[..]) };
$dest.write(<[$ty] as AsByteSlice<$ty>>::as_byte_slice(typed))
} else {
for item in $slice.iter() {
item.encode_to(dest);
}
}
}};
}
with_type_info! {
<T as Encode>::TYPE_INFO,
encode_to(slice, dest),
{
for item in slice.iter() {
item.encode_to(dest);
}
},
}
}
/// Decode the vec (without a prepended len).
///
/// This is equivalent to decode all elements one by one, but it is optimized in some
/// situation.
pub fn decode_vec_with_len<T: Decode, I: Input>(
input: &mut I,
len: usize,
) -> Result<Vec<T>, Error> {
fn decode_unoptimized<I: Input, T: Decode>(
input: &mut I,
items_len: usize,
) -> Result<Vec<T>, Error> {
let input_capacity = input.remaining_len()?
.unwrap_or(MAX_PREALLOCATION)
.checked_div(mem::size_of::<T>())
.unwrap_or(0);
let mut r = Vec::with_capacity(input_capacity.min(items_len));
input.descend_ref()?;
for _ in 0..items_len {
r.push(T::decode(input)?);
}
input.ascend_ref();
Ok(r)
}
macro_rules! decode {
( $ty:ty, $input:ident, $len:ident ) => {{
if cfg!(target_endian = "little") || mem::size_of::<T>() == 1 {
let vec = read_vec_from_u8s::<_, $ty>($input, $len)?;
Ok(unsafe { mem::transmute::<Vec<$ty>, Vec<T>>(vec) })
} else {
decode_unoptimized($input, $len)
}
}};
}
with_type_info! {
<T as Decode>::TYPE_INFO,
decode(input, len),
{
decode_unoptimized(input, len)
},
}
}
impl_for_non_zero! {
NonZeroI8,
NonZeroI16,
NonZeroI32,
NonZeroI64,
NonZeroI128,
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128,
}
impl<T: Encode, const N: usize> Encode for [T; N] {
fn size_hint(&self) -> usize {
mem::size_of::<T>() * N
}
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
encode_slice_no_len(&self[..], dest)
}
}
const fn calculate_array_bytesize<T, const N: usize>() -> usize {
struct AssertNotOverflow<T, const N: usize>(PhantomData<T>);
impl<T, const N: usize> AssertNotOverflow<T, N> {
const OK: () = assert!(mem::size_of::<T>().checked_mul(N).is_some(), "array size overflow");
}
#[allow(clippy::let_unit_value)]
let () = AssertNotOverflow::<T, N>::OK;
mem::size_of::<T>() * N
}
impl<T: Decode, const N: usize> Decode for [T; N] {
#[inline(always)]
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let mut array = MaybeUninit::uninit();
Self::decode_into(input, &mut array)?;
// SAFETY: `decode_into` succeeded, so the array is initialized.
unsafe {
Ok(array.assume_init())
}
}
fn decode_into<I: Input>(input: &mut I, dst: &mut MaybeUninit<Self>) -> Result<DecodeFinished, Error> {
let is_primitive = match <T as Decode>::TYPE_INFO {
| TypeInfo::U8
| TypeInfo::I8
=> true,
| TypeInfo::U16
| TypeInfo::I16
| TypeInfo::U32
| TypeInfo::I32
| TypeInfo::U64
| TypeInfo::I64
| TypeInfo::U128
| TypeInfo::I128
| TypeInfo::F32
| TypeInfo::F64
=> cfg!(target_endian = "little"),
TypeInfo::Unknown => false
};
if is_primitive {
// Let's read the array in bulk as that's going to be a lot
// faster than just reading each element one-by-one.
let ptr: *mut [T; N] = dst.as_mut_ptr();
let ptr: *mut u8 = ptr.cast();
let bytesize = calculate_array_bytesize::<T, N>();
// TODO: This is potentially slow; it'd be better if `Input` supported
// reading directly into uninitialized memory.
//
// SAFETY: The pointer is valid and points to a memory `bytesize` bytes big.
unsafe {
ptr.write_bytes(0, bytesize);
}
// SAFETY: We've zero-initialized everything so creating a slice here is safe.
let slice: &mut [u8] = unsafe {
core::slice::from_raw_parts_mut(ptr, bytesize)
};
input.read(slice)?;
// SAFETY: We've initialized the whole slice so calling this is safe.
unsafe {
return Ok(DecodeFinished::assert_decoding_finished());
}
}
let slice: &mut [MaybeUninit<T>; N] = {
let ptr: *mut [T; N] = dst.as_mut_ptr();
let ptr: *mut [MaybeUninit<T>; N] = ptr.cast();
// SAFETY: Casting `&mut MaybeUninit<[T; N]>` into `&mut [MaybeUninit<T>; N]` is safe.
unsafe { &mut *ptr }
};
/// A wrapper type to make sure the partially read elements are always
/// dropped in case an error occurs or the underlying `decode` implementation panics.
struct State<'a, T, const N: usize> {
count: usize,
slice: &'a mut [MaybeUninit<T>; N]
}
impl<'a, T, const N: usize> Drop for State<'a, T, N> {
fn drop(&mut self) {
if !mem::needs_drop::<T>() {
// If the types don't actually need to be dropped then don't even
// try to run the loop below.
//
// Most likely won't make a difference in release mode, but will
// make a difference in debug mode.
return;
}
// TODO: Use `MaybeUninit::slice_assume_init_mut` + `core::ptr::drop_in_place`
// once `slice_assume_init_mut` is stable.
for item in &mut self.slice[..self.count] {
// SAFETY: Each time we've read a new element we incremented `count`,
// and we only drop at most `count` elements here,
// so all of the elements we drop here are valid.
unsafe {
item.assume_init_drop();
}