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
use super::Expression;
use crate::Tuple;

/// Represents a single tuple of type `T`.
///
/// **Example**:
/// ```rust
/// use codd::{Database, expression::Singleton};
///
/// let mut db = Database::new();
/// let hello = Singleton::new("Hello".to_string());
///
/// assert_eq!(vec!["Hello".to_string()], db.evaluate(&hello).unwrap().into_tuples());
/// ```
#[derive(Clone, Debug)]
pub struct Singleton<T>(T)
where
    T: Tuple;

impl<T: Tuple> Singleton<T> {
    /// Create a new instance of `Singleton` with `tuple` as the inner value.
    pub fn new(tuple: T) -> Self {
        Self(tuple)
    }

    /// Returns a reference to the inner value of the receiver.
    #[inline(always)]
    pub fn tuple(&self) -> &T {
        &self.0
    }

    /// Consumes the receiver and returns its inner value.
    #[inline(always)]
    pub fn into_tuple(self) -> T {
        self.0
    }
}

impl<T> Expression<T> for Singleton<T>
where
    T: Tuple,
{
    fn visit<V>(&self, visitor: &mut V)
    where
        V: super::Visitor,
    {
        visitor.visit_singleton(&self)
    }
}

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

    #[test]
    fn test_new() {
        assert_eq!(42, Singleton::new(42).into_tuple());
    }

    #[test]
    fn test_clone() {
        let s = Singleton::new(42);
        assert_eq!(42, s.clone().into_tuple());
    }
}