ro 1.0.0

Helps make stuff read-only
Documentation
// License: see LICENSE file at root directory of `master` branch

//! # Read-only container

use core::{
    fmt::{self, Display, Formatter},
    ops::Deref,
};

/// # Read-only container
///
/// The key of this container is [`Deref`][core:Deref] implementation. It allows you to access public fields and immutable functions of `T`. In
/// case `T` has mutable functions, those are not accessible.
///
/// By design, the struct has no functions.
///
/// [core:Deref]: https://doc.rust-lang.org/core/ops/trait.Deref.html
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
pub struct ReadOnly<T> {
    data: T,
}

impl<T> Deref for ReadOnly<T> {

    type Target = T;

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

}

impl<T> Display for ReadOnly<T> where T: Display {

    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        self.data.fmt(f)
    }

}

impl<T> From<T> for ReadOnly<T> {

    fn from(data: T) -> Self {
        new(data)
    }

}

/// # Makes new read-only version of some value
pub const fn new<T>(some: T) -> ReadOnly<T> {
    ReadOnly {
        data: some,
    }
}