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
use crate::pool::{CheckOut, Dependencies};
use futures::Future;
use std::time::Instant;

pub enum Status {
    Valid,
    Invalid,
}

/// A trait for managing the lifecycle of a resource.
pub trait Manage: Sized {
    type Resource: Send;

    // TODO: Default to `RealDependencies` when associated type defaults are stabilized.
    type Dependencies: Dependencies;

    type CheckOut: From<CheckOut<Self>>;

    type Error;

    type CreateFuture: Future<Item = Self::Resource, Error = Self::Error>;

    /// Creates a new instance of the managed resource.
    fn create(&self) -> Self::CreateFuture;

    fn status(&self, resource: &Self::Resource) -> Status;

    type RecycleFuture: Future<Item = Option<Self::Resource>, Error = Self::Error>;

    /// Recycling a resource is done periodically to determine whether it is still valid and can be
    /// reused or if it is broken and must be discarded.
    fn recycle(&self, resource: Self::Resource) -> Self::RecycleFuture;
}

#[derive(Debug)]
pub struct Idle<R>
where
    R: Send,
{
    resource: R,
    recycled_at: Instant,
}

impl<R> Idle<R>
where
    R: Send,
{
    pub fn new(resource: R, recycled_at: Instant) -> Self {
        Self {
            resource,
            recycled_at,
        }
    }

    pub fn into_resource(self) -> R {
        self.resource
    }

    pub fn recycled_at(&self) -> Instant {
        self.recycled_at
    }
}