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
use futures::future::{ok, Ready};

use core::{default::Default as IDefault, ops::Deref};

use crate::{
    channel::Channel,
    kind,
    kind::{ConstructResult, DeconstructResult},
    Kind,
};

use super::{using, AsKind};

use void::Void;

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
pub struct Default<T: IDefault>(T);

impl<T: IDefault> Deref for Default<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: IDefault> From<T> for Default<T> {
    fn from(_: T) -> Self {
        Default::new()
    }
}

impl<T: IDefault + Unpin + Sync + Send + 'static> AsKind<using::Default> for T {
    type Kind = Default<T>;

    fn into_kind(self) -> Default<T> {
        Default::new()
    }
    fn from_kind(_: Self::Kind) -> Self {
        T::default()
    }
}

impl<T: IDefault> Default<T> {
    pub fn new() -> Self {
        Default::default()
    }
}

#[kind]
impl<T: IDefault + Unpin + Sync + Send + 'static> Kind for Default<T> {
    type ConstructItem = ();
    type ConstructError = Void;
    type ConstructFuture = Ready<ConstructResult<Self>>;
    type DeconstructItem = ();
    type DeconstructError = Void;
    type DeconstructFuture = Ready<DeconstructResult<Self>>;

    fn deconstruct<C: Channel<Self::DeconstructItem, Self::ConstructItem>>(
        self,
        _: C,
    ) -> Self::DeconstructFuture {
        ok(())
    }
    fn construct<C: Channel<Self::ConstructItem, Self::DeconstructItem>>(
        _: C,
    ) -> Self::ConstructFuture {
        ok(Default::new())
    }
}