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
#![allow(missing_docs)]

#[doc(hidden)] pub mod decode;
#[doc(hidden)] pub mod encode;
#[doc(hidden)] pub mod format;
#[doc(hidden)] pub mod future;
#[doc(hidden)] pub mod collection;
#[doc(hidden)] pub mod intrinsic;
#[doc(hidden)] pub mod primitive;
#[doc(hidden)] pub mod wrapper;

/// Types used to support structural serialization
pub mod internal;

use core::future::Future;
use crate::io;

#[doc(inline)]
pub use self::{
    decode::{
        Decode,
        PollDecodeStatus,
        StartDecodeStatus,
    },
    encode::{
        Encode,
        PollEncodeStatus,
        StartEncodeStatus,
    },
    format::{
        Format,
        FormatDecode,
        FormatDeserialize,
        FormatEncode,
        FormatSerialize
    },
    future::{
        deserialize_exact::DeserializeExact,
        serialize_all::SerializeAll,
    }
};

/// Define the [encoder](Encode) to use for serializing the data type.
pub trait Encodable
{
    /// The concrete [encoder](Encode) to use for serialization
    type Encoder<F>: Encode<Data=Self, Format=F>
    where
        F: FormatEncode,
    ;    
}

/// Define the [decoder](Decode) to use for deserializing the data type.
pub trait Decodable: Sized
{
    /// The concrete [decoder](Decode) to use for deserialization
    type Decoder<F>: Decode<Data=Self, Format=F>
    where
        F: FormatDecode,
    ;
}

/// Serialize a data structure asynchronously.
pub trait AsyncSerialize: Encodable
{
    /// The concrete [future](Future) returned by the `serialize` method.
    type Future<'w, F, W>: Future<Output=Result<(), F::Error>> + Unpin
    where
        Self: 'w,
        F: 'w + FormatSerialize,
        W: 'w + io::AsyncWrite + Unpin,
    ;

    /// Attempt to serialize the type asynchronously for the indicated [format](Format)
    /// via the provided [asynchronous writer](io::AsyncWrite).
    fn serialize<'w, F, W>(&'w self, format: &'w F, writer: &'w mut W) -> Self::Future<'w, F, W>
    where
        F: FormatSerialize,
        W: io::AsyncWrite + Unpin,
    ;
}

/// Deserialize a data structure asynchronously.
pub trait AsyncDeserialize: Decodable
{
    /// The concrete [future](Future) returned by the `deserialize` method.
    type Future<'r, F, R>: Future<Output=Result<Self, F::Error>> + Unpin
    where
        F: 'r + FormatDeserialize,
        R: 'r + io::AsyncBufRead + Unpin,
    ;

    /// Attempt to deserialize the type asynchronously for the indicated [format](Format)
    /// via the provided [asynchronous reader](io::AsyncBufRead).
    fn deserialize<'r, F, R>(format: &'r F, reader: &'r mut R) -> Self::Future<'r, F, R>
    where
        F: FormatDeserialize,
        R: io::AsyncBufRead + Unpin,
    ;
}

/// Marker trait to denote that both [AsyncSerialize] and [AsyncDeserialize]
/// are implemented for the type.
///
/// This is defined by default for any type that implements both
/// [AsyncSerialize] and [AsyncDeserialize].
pub trait AsyncSerialization: AsyncSerialize + AsyncDeserialize {}

impl<T> AsyncSerialization for T where T: AsyncSerialize + AsyncDeserialize {}