table 0.4.0

A specialized map for storing values of varying types.
Documentation
// Copyright (C) 2018  Project Tsukurou!
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

macro_rules! convert {
    (enum $enum:ty, variant $var:path: type $ty:ty) => {
        impl<'a> From<$ty> for $enum {
            fn from(value: $ty) -> $enum {
                $var(value)
            }
        }

        impl<'a> ::util::convert::TryAsRef<$ty> for $enum {
            fn try_as_ref(&self) -> Option<&$ty> {
                if let $var(value) = self {
                    Some(value)
                } else {
                    None
                }
            }
        }

        impl<'a> ::util::convert::TryAsMut<$ty> for $enum {
            fn try_as_mut(&mut self) -> Option<&mut $ty> {
                if let $var(value) = self {
                    Some(value)
                } else {
                    None
                }
            }
        }
    };

    (enum $enum:ty, type $into:ty: from $($from:ty),*) => {
        $(impl<'a> From<$from> for $enum {
            fn from(value: $from) -> $enum {
                <$enum>::from(<$into>::from(value))
            }
        })*
    };

    (enum $enum:ty, type $val:ty: ref $ref:ty) => {
        impl<'a> ::util::convert::TryAsRef<$ref> for $enum {
            fn try_as_ref(&self) -> Option<&$ref> {
                ::util::convert::TryAsRef::<$val>::try_as_ref(self)
                    .map(AsRef::as_ref)
            }
        }

        impl<'a> ::util::convert::TryAsMut<$ref> for $enum {
            fn try_as_mut(&mut self) -> Option<&mut $ref> {
                ::util::convert::TryAsMut::<$val>::try_as_mut(self)
                    .map(AsMut::as_mut)
            }
        }
    };

    (enum $enum:ty, type $into:ty: from_as $($from:ty),*) => {
        $(impl<'a> From<$from> for $enum {
            fn from(value: $from) -> $enum {
                <$enum>::from(value as $into)
            }
        })*
    };

    (enum $enum:ty: cow $r:ty, $t:ty) => {
        impl<'a> From<&'a $r> for $enum {
            fn from(value: &'a $r) -> $enum {
                <$enum>::from(Cow::Borrowed(value))
            }
        }

        impl<'a> From<$t> for $enum {
            fn from(value: $t) -> $enum {
                <$enum as From<Cow<'a, $r>>>::from(Cow::Owned(value))
            }
        }

        impl<'a> ::util::convert::TryAsRef<$r> for $enum {
            fn try_as_ref(&self) -> Option<&$r> {
                ::util::convert::TryAsRef::<::std::borrow::Cow<'a, $r>>
                    ::try_as_ref(self)
                    .map(::std::ops::Deref::deref)
            }
        }

        impl<'a> ::util::convert::TryAsMut<$t> for $enum {
            fn try_as_mut(&mut self) -> Option<&mut $t> { 
                ::util::convert::TryAsMut::<::std::borrow::Cow<'a, $r>>
                    ::try_as_mut(self)
                    .map(::std::borrow::Cow::to_mut)
            }
        }
    }
}