factory/
lib.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
//! This crate provides `Factory` trait and its implementations.
//!
//! The trait makes it possible to create any number of instances of a specific type.
//!
//! # Examples
//!
//! Creates default instances of `u8` type:
//!
//! ```
//! use factory::{DefaultFactory, Factory};
//!
//! let f = DefaultFactory::<u8>::new();
//! assert_eq!(f.create(), 0);
//! assert_eq!(f.create(), 0);
//! ```
#![warn(missing_docs)]

#[cfg(feature = "swappable")]
extern crate atomic_immut;

#[cfg(feature = "swappable")]
pub use swappable::SwappableFactory;

use std::marker::PhantomData;

#[cfg(feature = "swappable")]
mod swappable;

/// This trait allows for creating any number of instances of the `Item` type.
pub trait Factory {
    /// The type of instances created by this factory.
    type Item;

    /// Creates an instance.
    fn create(&self) -> Self::Item;
}
impl<T: ?Sized + Factory> Factory for &T {
    type Item = T::Item;

    fn create(&self) -> Self::Item {
        (**self).create()
    }
}
impl<T: ?Sized + Factory> Factory for Box<T> {
    type Item = T::Item;

    fn create(&self) -> Self::Item {
        (**self).create()
    }
}

/// This trait allows for creating any number of instances of the `Item` type with the given parameter.
pub trait ParameterizedFactory {
    /// The type of instances created by this factory.
    type Item;

    /// The type of parameter.
    type Parameter;

    /// Creates an instance.
    fn create(&self, param: Self::Parameter) -> Self::Item;
}
impl<T: ?Sized + ParameterizedFactory> ParameterizedFactory for &T {
    type Item = T::Item;
    type Parameter = T::Parameter;

    fn create(&self, param: Self::Parameter) -> Self::Item {
        (**self).create(param)
    }
}
impl<T: ?Sized + ParameterizedFactory> ParameterizedFactory for Box<T> {
    type Item = T::Item;
    type Parameter = T::Parameter;

    fn create(&self, param: Self::Parameter) -> Self::Item {
        (**self).create(param)
    }
}

/// A `Factory` that creates instances using `T::default()` function.
///
/// # Examples
///
/// ```
/// use factory::{DefaultFactory, Factory};
///
/// let f = DefaultFactory::<u8>::new();
/// assert_eq!(f.create(), 0);
/// ```
#[derive(Debug, Default)]
pub struct DefaultFactory<T>(PhantomData<T>);
impl<T: Default> DefaultFactory<T> {
    /// Makes a new `DefaultFactory`.
    pub fn new() -> Self {
        DefaultFactory(PhantomData)
    }
}
impl<T: Default> Factory for DefaultFactory<T> {
    type Item = T;

    fn create(&self) -> Self::Item {
        T::default()
    }
}
impl<T> Clone for DefaultFactory<T> {
    fn clone(&self) -> Self {
        DefaultFactory(PhantomData)
    }
}
unsafe impl<T> Send for DefaultFactory<T> {}
unsafe impl<T> Sync for DefaultFactory<T> {}

/// A `Factory` that creates instances using `T::clone()` method.
///
/// # Examples
///
/// ```
/// use factory::{CloneFactory, Factory};
///
/// let f = CloneFactory::new(10);
/// assert_eq!(f.create(), 10);
/// ```
#[derive(Debug, Default, Clone)]
pub struct CloneFactory<T>(T);
impl<T: Clone> CloneFactory<T> {
    /// Makes a new `CloneFactory`.
    ///
    /// The instances the factory creates are copied from `original`.
    pub fn new(original: T) -> Self {
        CloneFactory(original)
    }

    /// Returns a reference to the original instance.
    pub fn get_ref(&self) -> &T {
        &self.0
    }

    /// Returns a mutable reference to the original instance.
    pub fn get_mut(&mut self) -> &mut T {
        &mut self.0
    }
}
impl<T: Clone> Factory for CloneFactory<T> {
    type Item = T;

    fn create(&self) -> Self::Item {
        self.0.clone()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn default_factory_works() {
        let f = DefaultFactory::<u8>::new();
        assert_eq!(f.create(), 0);
        assert_eq!(f.clone().create(), 0);
    }

    #[test]
    fn clone_factory_works() {
        let mut f = CloneFactory::new(32);
        assert_eq!(f.get_ref(), &32);
        assert_eq!(f.create(), 32);

        *f.get_mut() = 50;
        assert_eq!(f.create(), 50);
    }
}