#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![no_std]
#[cfg(feature = "alloc")]
extern crate alloc;
mod convert;
mod derive;
mod fmt;
use core::error::Error;
use core::ops::Index;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[derive(Debug)]
pub enum Flex<'a, T: ?Sized> {
Lend(&'a T),
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
Give(Box<T>),
}
impl<'a, T: ?Sized> Default for Flex<'a, T>
where
&'a T: Default,
{
fn default() -> Self {
Flex::Lend(Default::default())
}
}
impl<'a, T: ?Sized + Index<I>, I> Index<I> for Flex<'a, T> {
type Output = T::Output;
fn index(&self, index: I) -> &Self::Output {
Index::index(&**self, index)
}
}
impl<'a, T: ?Sized> IntoIterator for &'a Flex<'a, T>
where
&'a T: IntoIterator,
{
type Item = <&'a T as IntoIterator>::Item;
type IntoIter = <&'a T as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
(**self).into_iter()
}
}
impl<'a, T: ?Sized + Error> Error for Flex<'a, T> {}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<'a, T: ?Sized> Flex<'a, T>
where
Box<T>: From<&'a T>,
{
pub fn into_box(self) -> Box<T> {
match self {
Flex::Lend(r) => Box::from(r),
Flex::Give(b) => b,
}
}
pub fn claim<'b>(self) -> Flex<'b, T> {
Flex::Give(self.into_box())
}
}