spaces 4.1.0

Set/space primitives for defining machine learning problems.
Documentation
use core::{Space, Card, Surjection};
use rand::Rng;
use std::fmt;

/// A null dimension.
#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
pub struct Null;

impl Space for Null {
    type Value = ();

    fn dim(&self) -> usize { 1 }

    fn card(&self) -> Card { Card::Null }

    fn sample<R: Rng + ?Sized>(&self, _: &mut R) -> () { () }
}

impl<T> Surjection<T, ()> for Null {
    fn map(&self, _: T) -> () { () }
}

impl fmt::Display for Interval {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "\u{2205}")
    }
}

#[cfg(test)]
mod tests {
    extern crate serde_test;

    use self::serde_test::{assert_tokens, Token};
    use super::*;
    use rand::{thread_rng, Rng};

    #[test]
    fn test_card() {
        let d = Null;

        assert_eq!(d.card(), Card::Null);
    }

    #[test]
    fn test_sampling() {
        let d = Null;
        let mut rng = thread_rng();

        for _ in 0..10 {
            assert_eq!(d.sample(&mut rng), ());
        }
    }

    #[test]
    fn test_surjection() {
        let d = Null;
        let mut rng = thread_rng();

        for _ in 0..10 {
            assert_eq!(d.map(rng.gen::<f64>()), ());
            assert_eq!(d.map(rng.gen::<u64>()), ());
        }
    }

    #[test]
    fn test_serialisation() {
        let d = Null;

        assert_tokens(&d, &[Token::UnitStruct { name: "Null" }]);
    }
}