bincode-next 3.0.0-rc.11

A compact, ultra-fast binary serialization format for Rust, optimized for networking and storage!
Documentation
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
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(internal_features)]
#![cfg_attr(is_nightly, feature(core_intrinsics))]

//! Bincode-next is a crate for encoding and decoding using a tiny binary
//! serialization strategy.  Using it, you can easily go from having
//! an object in memory, quickly serialize it to bytes, and then
//! deserialize it back just as fast!
//!
//! If you're coming from bincode 1, check out our [migration guide](migration_guide/index.html)
//!
//! # Serde
//!
//! Starting from bincode 2, serde is now an optional dependency. If you want to use serde, please enable the `serde` feature. See [Features](#features) for more information.
//!
//! # Features
//!
//! |Name  |Default?|Affects MSRV?|Supported types for Encode/Decode|Enabled methods                                                  |Other|
//! |------|--------|-------------|-----------------------------------------|-----------------------------------------------------------------|-----|
//! |std   | Yes    | No          |`HashMap` and `HashSet`|`decode_from_std_read` and `encode_into_std_write`|
//! |alloc | Yes    | No          |All common containers in alloc, like `Vec`, `String`, `Box`|`encode_to_vec`|
//! |atomic| Yes    | No          |All `Atomic*` integer types, e.g. `AtomicUsize`, and `AtomicBool`||
//! |derive| Yes    | No          |||Enables the `BorrowDecode`, `Decode`, `Encode`, `Fingerprint` and `BitPacked` derive macros|
//! |serde | No     | Yes (MSRV reliant on serde)|`Compat` and `BorrowCompat`, which will work for all types that implement serde's traits|serde-specific encode/decode functions in the [`serde`\] module|Note: There are several [known issues](serde/index.html#known-issues) when using serde and bincode|
//! |zero-copy| No    | No          |`RelativePtr`, `ZeroArray`, `ZeroSlice`, `ZeroStr`, `ZeroString`|Enables the `relative_ptr` module and the `ZeroCopy` derive macro|Zero-copy nested structures using offsets|
//! |static-size| No    | No          |||Enables the `static_size` module, the `bounded` module and the `StaticSize` derive macro|Compile-time size verification|
//!
//! # Which functions to use
//!
//! Bincode has a couple of pairs of functions that are used in different situations.
//!
//! |Situation|Encode|Decode|
//! |---|---|---
//! |You're working with [`fs::File`\] or [`net::TcpStream`\]|[`encode_into_std_write`\]|[`decode_from_std_read`\]|
//! |you're working with in-memory buffers|[`encode_to_vec`\]|[`decode_from_slice`\]|
//! |You want to use a custom [Reader] and [Writer]|[`encode_into_writer`\]|[`decode_from_reader`\]|
//! |You're working with pre-allocated buffers or on embedded targets|[`encode_into_slice`\]|[`decode_from_slice`\]|
//!
//! **Note:** If you're using `serde`, use `bincode_next::serde::...` instead of `bincode_next::...`
//!
//! # Example
//!
//! ```rust
//! let mut slice = [0u8; 100];
//!
//! // You can encode any type that implements `Encode`.
//! // You can automatically implement this trait on custom types with the `derive` feature.
//! let input = (
//!     0u8,
//!     10u32,
//!     10000i128,
//!     'a',
//!     [0u8, 1u8, 2u8, 3u8]
//! );
//!
//! let length = bincode_next::encode_into_slice(
//!     input,
//!     &mut slice,
//!     bincode_next::config::standard()
//! ).unwrap();
//!
//! let slice = &slice[..length];
//! println!("Bytes written: {:?}", slice);
//!
//! // Decoding works the same as encoding.
//! // The trait used is `Decode`, and can also be automatically implemented with the `derive` feature.
//! let decoded: (u8, u32, i128, char, [u8; 4]) = bincode_next::decode_from_slice(slice, bincode_next::config::standard()).unwrap().0;
//!
//! assert_eq!(decoded, input);
//! ```
//!
//! [`fs::File`\]: `std::fs::File`
//! [`net::TcpStream`\]: `std::net::TcpStream`

// =========================================================================
// RUST LINT CONFIGURATION: bincode-next
// =========================================================================

// -------------------------------------------------------------------------
// LEVEL 1: CRITICAL ERRORS (Deny)
// -------------------------------------------------------------------------
#![deny(
    // Rust Compiler Errors
    unreachable_code,
    improper_ctypes_definitions,
    future_incompatible,
    nonstandard_style,
    rust_2018_idioms,
    clippy::perf,
    clippy::correctness,
    clippy::suspicious,
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::arithmetic_side_effects,
    clippy::missing_safety_doc,
    clippy::same_item_push,
    clippy::implicit_clone,
    clippy::all,
    clippy::pedantic,
    missing_docs,
    clippy::nursery,
    clippy::single_call_fn,
)]
// -------------------------------------------------------------------------
// LEVEL 2: STYLE WARNINGS (Warn)
// -------------------------------------------------------------------------
#![warn(
    // For `no-std` Situation Issues
    dead_code,
    warnings,
    unsafe_code,
    clippy::dbg_macro,
    clippy::todo,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap,
    clippy::unnecessary_safety_comment
)]
// -------------------------------------------------------------------------
// LEVEL 3: ALLOW/IGNORABLE (Allow)
// -------------------------------------------------------------------------
#![allow(
    clippy::restriction,
    clippy::inline_always,
    unused_doc_comments,
    clippy::empty_line_after_doc_comments
)]
#![crate_name = "bincode_next"]
#![crate_type = "rlib"]

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;

mod atomic;
#[doc(hidden)]
pub mod error_path;
mod features;
#[doc(hidden)]
pub mod utils;
pub(crate) mod varint;

use de::Decoder;
use de::read::Reader;
use enc::write::Writer;

#[cfg(any(
    feature = "alloc",
    feature = "std",
    feature = "derive",
    feature = "serde",
    feature = "zero-copy",
    feature = "static-size"
))]
pub use features::*;

/// The major version of the bincode library.
pub const BINCODE_MAJOR_VERSION: u64 = 3;

#[doc(hidden)]
pub use rapidhash;

pub mod config;
/// Fingerprinting support for schema verification.
pub mod fingerprint;

#[macro_use]
pub mod de;
pub mod enc;
pub mod error;

#[cfg(feature = "static-size")]
pub use static_size::StaticSize;

pub use de::BorrowDecode;
pub use de::Decode;
pub use enc::Encode;
pub use fingerprint::Fingerprint;
#[cfg(feature = "zero-copy")]
pub use relative_ptr::ZeroCopy;
#[cfg(feature = "zero-copy")]
pub use relative_ptr::ZeroCopyType;

#[cfg(not(any(feature = "std", feature = "serde")))]
#[cfg(feature = "no-std")]
use panic_halt as _;

use config::Config;
use config::internal::InternalFingerprintGuard;

/// Encode the given value into the given slice. Returns the amount of bytes that have been written.
///
/// See the [config] module for more information on configurations.
///
/// # Errors
///
/// Returns an `EncodeError` if the slice is too small or the value cannot be encoded.
///
/// [config]: config/index.html
#[inline]
pub fn encode_into_slice<E: enc::Encode, C: Config>(
    val: E,
    dst: &mut [u8],
    config: C,
) -> Result<usize, error::EncodeError>
where
    C::Mode: config::InternalFingerprintGuard<E, C>,
{
    let mut writer = enc::write::SliceWriter::new(dst);
    C::Mode::encode_check(&config, &mut writer)?;
    let mut encoder = enc::EncoderImpl::<_, C>::new(writer, config);
    val.encode(&mut encoder)?;
    Ok(encoder.into_writer().bytes_written())
}

/// Encode the given value into a custom [`Writer`\].
///
/// See the [config] module for more information on configurations.
///
/// # Errors
///
/// Returns an `EncodeError` if the writer fails or the value cannot be encoded.
///
/// [config]: config/index.html
#[inline]
pub fn encode_into_writer<E: enc::Encode, W: Writer, C: Config>(
    val: E,
    mut writer: W,
    config: C,
) -> Result<(), error::EncodeError>
where
    C::Mode: config::InternalFingerprintGuard<E, C>,
{
    C::Mode::encode_check(&config, &mut writer)?;
    let mut encoder = enc::EncoderImpl::<_, C>::new(writer, config);
    val.encode(&mut encoder)?;
    Ok(())
}

/// Attempt to decode a given type `D` from the given slice. Returns the decoded output and the amount of bytes read.
///
/// Note that this does not work with borrowed types like `&str` or `&[u8]`. For that use [`borrow_decode_from_slice`\].
///
/// See the [config] module for more information on configurations.
///
/// # Errors
///
/// Returns a `DecodeError` if the slice is too small or the data is invalid.
///
/// [config]: config/index.html
#[inline(always)]
pub fn decode_from_slice<D: de::Decode<()>, C: Config>(
    src: &[u8],
    config: C,
) -> Result<(D, usize), error::DecodeError>
where
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    decode_from_slice_with_context(src, config, ())
}

/// Attempt to decode a given type `D` from the given slice with `Context`. Returns the decoded output and the amount of bytes read.
///
/// Note that this does not work with borrowed types like `&str` or `&[u8]`. For that use [`borrow_decode_from_slice`\].
///
/// See the [config] module for more information on configurations.
///
/// # Errors
///
/// Returns a `DecodeError` if the slice is too small or the data is invalid.
///
/// [config]: config/index.html
#[inline]
pub fn decode_from_slice_with_context<Context, D: de::Decode<Context>, C: Config>(
    src: &[u8],
    config: C,
    context: Context,
) -> Result<(D, usize), error::DecodeError>
where
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    let mut reader = de::read::SliceReader::new(src);
    C::Mode::decode_check(&config, &mut reader)?;
    let mut decoder = de::DecoderImpl::<_, C, Context>::new(reader, config, context);
    let result = D::decode(&mut decoder)?;
    let bytes_read = src.len() - decoder.reader().slice.len();
    Ok((result, bytes_read))
}

/// Attempt to decode a given type `D` from the given slice. Returns the decoded output and the amount of bytes read.
///
/// See the [config] module for more information on configurations.
///
/// # Errors
///
/// Returns a `DecodeError` if the slice is too small or the data is invalid.
///
/// [config]: config/index.html
#[inline(always)]
pub fn borrow_decode_from_slice<'a, D: de::BorrowDecode<'a, ()>, C: Config>(
    src: &'a [u8],
    config: C,
) -> Result<(D, usize), error::DecodeError>
where
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    borrow_decode_from_slice_with_context(src, config, ())
}

/// Attempt to decode a given type `D` from the given slice with `Context`. Returns the decoded output and the amount of bytes read.
///
/// See the [config] module for more information on configurations.
///
/// # Errors
///
/// Returns a `DecodeError` if the slice is too small or the data is invalid.
///
/// [config]: config/index.html
#[inline]
pub fn borrow_decode_from_slice_with_context<
    'a,
    Context,
    D: de::BorrowDecode<'a, Context>,
    C: Config,
>(
    src: &'a [u8],
    config: C,
    context: Context,
) -> Result<(D, usize), error::DecodeError>
where
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    let mut reader = de::read::SliceReader::new(src);
    C::Mode::decode_check(&config, &mut reader)?;
    let mut decoder = de::DecoderImpl::<_, C, Context>::new(reader, config, context);
    let result = D::borrow_decode(&mut decoder)?;
    let bytes_read = src.len() - decoder.reader().slice.len();
    Ok((result, bytes_read))
}

/// Attempt to decode a given type `D` from the given slice with a compile-time bound check.
///
/// This function ensures that the target type `D` cannot exceed the provided buffer capacity `CAP` at compile-time.
///
/// # Errors
///
/// Returns a `DecodeError` if the slice contains invalid data.
#[cfg(feature = "static-size")]
#[inline(always)]
pub fn decode_from_slice_static<D, const CAP: usize, C>(
    src: &[u8; CAP],
    config: C,
) -> Result<D, error::DecodeError>
where
    D: de::Decode<()> + static_size::StaticSize,
    C: Config,
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    const {
        assert!(D::MAX_SIZE <= CAP, "Buffer too small for target type");
    }
    let (val, _) = decode_from_slice(src, config)?;
    Ok(val)
}

/// Attempt to decode a given type `D` from the given slice with a compile-time bound check and a
/// decoding context.
///
/// This function ensures that the target type `D` cannot exceed the provided buffer capacity `CAP`
/// at compile-time.
///
/// # Errors
///
/// Returns a `DecodeError` if the slice contains invalid data.
#[cfg(feature = "static-size")]
#[inline(always)]
pub fn decode_from_slice_static_with_context<Context, D, const CAP: usize, C>(
    src: &[u8; CAP],
    config: C,
    context: Context,
) -> Result<D, error::DecodeError>
where
    D: de::Decode<Context> + static_size::StaticSize,
    C: Config,
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    const {
        assert!(D::MAX_SIZE <= CAP, "Buffer too small for target type");
    }
    let (val, _) = decode_from_slice_with_context(src, config, context)?;
    Ok(val)
}

/// Attempt to decode a given type `D` from the given slice with a compile-time bound check.
///
/// This function ensures that the target type `D` cannot exceed the provided buffer capacity `CAP`
/// at compile-time.
///
/// # Errors
///
/// Returns a `DecodeError` if the slice contains invalid data.
#[cfg(feature = "static-size")]
#[inline(always)]
pub fn borrow_decode_from_slice_static<'a, D, const CAP: usize, C>(
    src: &'a [u8; CAP],
    config: C,
) -> Result<D, error::DecodeError>
where
    D: de::BorrowDecode<'a, ()> + static_size::StaticSize,
    C: Config,
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    const {
        assert!(D::MAX_SIZE <= CAP, "Buffer too small for target type");
    }
    let (val, _) = borrow_decode_from_slice(src, config)?;
    Ok(val)
}

/// Attempt to borrow-decode a given type `D` from the given slice with a compile-time bound check
/// and a decoding context.
///
/// This function ensures that the target type `D` cannot exceed the provided buffer capacity `CAP`
/// at compile-time.
///
/// # Errors
///
/// Returns a `DecodeError` if the slice contains invalid data.
#[cfg(feature = "static-size")]
#[inline(always)]
pub fn borrow_decode_from_slice_static_with_context<'a, Context, D, const CAP: usize, C>(
    src: &'a [u8; CAP],
    config: C,
    context: Context,
) -> Result<D, error::DecodeError>
where
    D: de::BorrowDecode<'a, Context> + static_size::StaticSize,
    C: Config,
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    const {
        assert!(D::MAX_SIZE <= CAP, "Buffer too small for target type");
    }
    let (val, _) = borrow_decode_from_slice_with_context(src, config, context)?;
    Ok(val)
}

/// Attempt to decode a given type `D` from the given [`Reader`\].
///
/// See the [config] module for more information on configurations.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[inline]
pub fn decode_from_reader<D: de::Decode<()>, R: Reader, C: Config>(
    mut reader: R,
    config: C,
) -> Result<D, error::DecodeError>
where
    C::Mode: config::InternalFingerprintGuard<D, C>,
{
    C::Mode::decode_check(&config, &mut reader)?;
    let mut decoder = de::DecoderImpl::<_, C, ()>::new(reader, config, ());
    D::decode(&mut decoder)
}

/// Attempt to decode a given type `T` from the given async reader safely using a non-blocking fiber.
///
/// Requires the `async-fiber` feature.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(feature = "async-fiber")]
#[inline(always)]
pub async fn decode_async<T, R, C>(
    config: C,
    reader: R,
) -> Result<T, crate::error::DecodeError>
where
    T: crate::Decode<()>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
    C::Mode: crate::config::InternalFingerprintGuard<T, C>,
{
    decode_async_with_context::<T, R, C, ()>(config, reader, ()).await
}

/// Attempt to decode a given type `T` from the given async reader using a non-blocking fiber and a context.
///
/// This is the primary implementation for runtimes that use the `futures-io` traits (e.g. `async-std`, `smol`).
///
/// Requires the `async-fiber` feature.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(feature = "async-fiber")]
#[inline]
pub async fn decode_async_with_context<T, R, C, Context>(
    config: C,
    reader: R,
    context: Context,
) -> Result<T, crate::error::DecodeError>
where
    T: crate::Decode<Context>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
    C::Mode: crate::config::InternalFingerprintGuard<T, C>,
{
    let bridge = crate::de::async_fiber::AsyncFiberBridge::new(reader);
    bridge
        .run(move |fiber_reader| {
            C::Mode::decode_check(&config, fiber_reader)?;
            let mut decoder =
                crate::de::DecoderImpl::<_, C, Context>::new(fiber_reader, config, context);
            T::decode(&mut decoder)
        })
        .await
}

/// Attempt to decode a given type `T` from an `async-std` reader.
///
/// Requires the `async-fiber` feature.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(feature = "async-fiber")]
#[inline(always)]
pub async fn decode_async_std<T, R, C>(
    config: C,
    reader: R,
) -> Result<T, crate::error::DecodeError>
where
    T: crate::Decode<()>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
    C::Mode: crate::config::InternalFingerprintGuard<T, C>,
{
    decode_async_with_context::<T, R, C, ()>(config, reader, ()).await
}

/// Attempt to decode a given type `T` from an `async-std` reader.
///
/// Requires the `async-fiber` feature.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(feature = "async-fiber")]
#[inline(always)]
pub async fn decode_async_std_with_context<T, R, C, Context>(
    config: C,
    reader: R,
    context: Context,
) -> Result<T, crate::error::DecodeError>
where
    T: crate::Decode<Context>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
    C::Mode: crate::config::InternalFingerprintGuard<T, C>,
{
    decode_async_with_context::<T, R, C, Context>(config, reader, context).await
}

/// Attempt to decode a given type `T` from a `smol` reader.
///
/// Requires the `async-fiber` feature.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(feature = "async-fiber")]
#[inline(always)]
pub async fn decode_async_smol<T, R, C>(
    config: C,
    reader: R,
) -> Result<T, crate::error::DecodeError>
where
    T: crate::Decode<()>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
    C::Mode: crate::config::InternalFingerprintGuard<T, C>,
{
    decode_async_with_context::<T, R, C, ()>(config, reader, ()).await
}

/// Attempt to decode a given type `T` from a `smol` reader.
///
/// Requires the `async-fiber` feature.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(feature = "async-fiber")]
#[inline(always)]
pub async fn decode_async_smol_with_context<T, R, C, Context>(
    config: C,
    reader: R,
    context: Context,
) -> Result<T, crate::error::DecodeError>
where
    T: crate::Decode<Context>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
    C::Mode: crate::config::InternalFingerprintGuard<T, C>,
{
    decode_async_with_context::<T, R, C, Context>(config, reader, context).await
}

/// Attempt to decode a given type `T` from the given tokio async reader safely using a non-blocking fiber.
///
/// Requires the `tokio` and `async-fiber` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "tokio", feature = "async-fiber"))]
#[inline(always)]
pub async fn decode_tokio_async<T, R, C>(
    config: C,
    reader: R,
) -> Result<T, crate::error::DecodeError>
where
    T: crate::Decode<()>,
    R: tokio::io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
    C::Mode: crate::config::InternalFingerprintGuard<T, C>,
{
    decode_async_tokio_with_context::<T, R, C, ()>(config, reader, ()).await
}

/// Attempt to decode a given type `T` from the given tokio async reader using a non-blocking fiber and a context.
///
/// Requires the `tokio` and `async-fiber` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "tokio", feature = "async-fiber"))]
#[inline(always)]
pub async fn decode_async_tokio_with_context<T, R, C, Context>(
    config: C,
    reader: R,
    context: Context,
) -> Result<T, crate::error::DecodeError>
where
    T: crate::Decode<Context>,
    R: tokio::io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
    C::Mode: crate::config::InternalFingerprintGuard<T, C>,
{
    let reader = crate::de::async_fiber::TokioReader(reader);
    decode_async_with_context::<T, _, C, Context>(config, reader, context).await
}

/// Attempt to decode a given serde-compatible type `T` from the given async reader safely using a non-blocking fiber.
///
/// Requires the `async-fiber` and `serde` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "async-fiber", feature = "serde"))]
#[inline(always)]
pub async fn decode_serde_async<'de, T, R, C>(
    config: C,
    reader: R,
) -> Result<T, crate::error::DecodeError>
where
    T: ::serde::Deserialize<'de>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
{
    decode_serde_async_with_context::<'de, T, R, C, ()>(config, reader, ()).await
}

/// Attempt to decode a given serde-compatible type `T` from an `async-std` reader.
///
/// Requires the `async-fiber` and `serde` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "async-fiber", feature = "serde"))]
#[inline(always)]
pub async fn decode_serde_async_std<'de, T, R, C>(
    config: C,
    reader: R,
) -> Result<T, crate::error::DecodeError>
where
    T: ::serde::Deserialize<'de>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
{
    decode_serde_async_with_context::<'de, T, R, C, ()>(config, reader, ()).await
}

/// Attempt to decode a given serde-compatible type `T` from an `async-std` reader.
///
/// Requires the `async-fiber` and `serde` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "async-fiber", feature = "serde"))]
#[inline(always)]
pub async fn decode_serde_async_std_with_context<'de, T, R, C, Context>(
    config: C,
    reader: R,
    context: Context,
) -> Result<T, crate::error::DecodeError>
where
    T: ::serde::Deserialize<'de>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
{
    decode_serde_async_with_context::<'de, T, R, C, Context>(config, reader, context).await
}

/// Attempt to decode a given serde-compatible type `T` from a `smol` reader.
///
/// Requires the `async-fiber` and `serde` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "async-fiber", feature = "serde"))]
#[inline(always)]
pub async fn decode_serde_async_smol<'de, T, R, C>(
    config: C,
    reader: R,
) -> Result<T, crate::error::DecodeError>
where
    T: ::serde::Deserialize<'de>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
{
    decode_serde_async_with_context::<'de, T, R, C, ()>(config, reader, ()).await
}

/// Attempt to decode a given serde-compatible type `T` from a `smol` reader.
///
/// Requires the `async-fiber` and `serde` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "async-fiber", feature = "serde"))]
#[inline(always)]
pub async fn decode_serde_async_smol_with_context<'de, T, R, C, Context>(
    config: C,
    reader: R,
    context: Context,
) -> Result<T, crate::error::DecodeError>
where
    T: ::serde::Deserialize<'de>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
{
    decode_serde_async_with_context::<'de, T, R, C, Context>(config, reader, context).await
}

/// Attempt to decode a given serde-compatible type `T` from the given async reader using a non-blocking fiber and a context.
///
/// Requires the `async-fiber` and `serde` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "async-fiber", feature = "serde"))]
#[inline]
pub async fn decode_serde_async_with_context<'de, T, R, C, Context>(
    config: C,
    reader: R,
    context: Context,
) -> Result<T, crate::error::DecodeError>
where
    T: ::serde::Deserialize<'de>,
    R: futures_io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
{
    let bridge = crate::de::async_fiber::AsyncFiberBridge::new(reader);
    bridge
        .run(move |fiber_reader| {
            let decoder =
                crate::de::DecoderImpl::<_, C, Context>::new(fiber_reader, config, context);
            let mut serde_decoder = crate::features::serde::OwnedSerdeDecoder { de: decoder };
            T::deserialize(serde_decoder.as_deserializer())
        })
        .await
}

/// Attempt to decode a given serde-compatible type `T` from the given tokio async reader safely using a non-blocking fiber.
///
/// Requires the `tokio`, `async-fiber` and `serde` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "tokio", feature = "async-fiber", feature = "serde"))]
#[inline(always)]
pub async fn decode_serde_tokio_async<'de, T, R, C>(
    config: C,
    reader: R,
) -> Result<T, crate::error::DecodeError>
where
    T: ::serde::Deserialize<'de>,
    R: tokio::io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
{
    decode_serde_tokio_async_with_context::<'de, T, R, C, ()>(config, reader, ()).await
}

/// Attempt to decode a given serde-compatible type `T` from the given tokio async reader using a non-blocking fiber and a context.
///
/// Requires the `tokio`, `async-fiber` and `serde` features.
///
/// # Errors
///
/// Returns a `DecodeError` if the reader fails or the data is invalid.
///
/// [config]: config/index.html
#[cfg(all(feature = "tokio", feature = "async-fiber", feature = "serde"))]
#[inline(always)]
pub async fn decode_serde_tokio_async_with_context<'de, T, R, C, Context>(
    config: C,
    reader: R,
    context: Context,
) -> Result<T, crate::error::DecodeError>
where
    T: ::serde::Deserialize<'de>,
    R: tokio::io::AsyncRead + std::marker::Unpin,
    C: crate::config::Config,
{
    let reader = crate::de::async_fiber::TokioReader(reader);
    decode_serde_async_with_context::<'de, T, _, C, Context>(config, reader, context).await
}


#[cfg(all(feature = "alloc", feature = "derive", doc))]
pub mod spec {
    #![doc = include_str!("../docs/spec.md")]
}

#[cfg(doc)]
pub mod migration_guide {
    #![doc = include_str!("../docs/migration_guide.md")]
}

// Test the examples in readme.md
#[cfg(all(
    feature = "std",
    feature = "derive",
    feature = "serde",
    feature = "async-fiber",
    doctest
))]
#[cfg_attr(miri, ignore)]
mod readme {
    #![doc = include_str!("../README.md")]
}