#![no_std]
#![warn(unsafe_op_in_unsafe_fn)]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
mod as_ref;
mod cmp;
mod extend;
mod from;
mod from_iter;
mod from_str;
mod imp;
mod serde;
mod to_boxed;
use core::borrow::Borrow;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::marker::Unpin;
use core::ops::Deref;
use alloc::boxed::Box;
#[cfg(feature = "std")]
use std::{
ffi::{CStr, OsStr},
path::Path,
};
use crate::imp::Cow as _;
pub use crate::imp::Dairy;
pub use crate::to_boxed::ToBoxed;
pub type String<'a> = Cow<'a, str>;
pub type Vec<'a, T> = Cow<'a, [T]>;
#[cfg(feature = "std")]
pub type CString<'a> = Cow<'a, CStr>;
#[cfg(feature = "std")]
pub type OsString<'a> = Cow<'a, OsStr>;
#[cfg(feature = "std")]
pub type PathBuf<'a> = Cow<'a, Path>;
pub struct Cow<'a, T>
where
T: ?Sized + Dairy<'a>,
{
inner: T::Cow,
}
impl<'a, T> Cow<'a, T>
where
T: ?Sized + Dairy<'a>,
{
#[inline]
pub fn borrowed(b: &'a T) -> Self {
Self {
inner: T::Cow::borrowed(b),
}
}
#[inline]
pub fn owned(o: T::Owned) -> Self {
Self {
inner: T::Cow::owned(o),
}
}
#[inline]
pub fn is_borrowed(&self) -> bool {
self.inner.is_borrowed()
}
#[inline]
pub fn is_owned(&self) -> bool {
self.inner.is_owned()
}
#[inline]
fn make_ref(&self) -> &T {
self.inner.make_ref()
}
#[inline]
pub fn into_owned(self) -> T::Owned {
self.inner.into_owned()
}
#[inline]
pub fn into_boxed(self) -> Box<T>
where
T: ToBoxed,
{
T::to_boxed(self.into_owned())
}
#[inline]
pub fn apply<F>(&mut self, f: F)
where
F: FnOnce(&mut T::Owned),
{
self.inner.apply(f)
}
}
impl<'a, T> Deref for Cow<'a, T>
where
T: ?Sized + Dairy<'a>,
{
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.make_ref()
}
}
impl<'a, T> Borrow<T> for Cow<'a, T>
where
T: ?Sized + Dairy<'a>,
{
#[inline]
fn borrow(&self) -> &T {
self.make_ref()
}
}
impl<'a, T> Clone for Cow<'a, T>
where
T: ?Sized + Dairy<'a>,
{
#[inline]
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<'a, T> fmt::Debug for Cow<'a, T>
where
T: ?Sized + Dairy<'a> + fmt::Debug,
T::Owned: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<'a, T> fmt::Display for Cow<'a, T>
where
T: ?Sized + Dairy<'a> + fmt::Display,
T::Owned: fmt::Display,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
impl<'a, T> Default for Cow<'a, T>
where
T: ?Sized + Dairy<'a>,
T::Owned: Default,
{
#[inline]
fn default() -> Self {
Self::owned(T::Owned::default())
}
}
impl<'a, T> Hash for Cow<'a, T>
where
T: ?Sized + Dairy<'a> + Hash,
{
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(&**self, state)
}
}
unsafe impl<'a, T> Send for Cow<'a, T>
where
T: ?Sized + Dairy<'a> + Sync,
T::Owned: Send,
{
}
unsafe impl<'a, T> Sync for Cow<'a, T>
where
T: ?Sized + Dairy<'a> + Sync,
T::Owned: Sync,
{
}
impl<'a, T> Unpin for Cow<'a, T>
where
T: ?Sized + Dairy<'a>,
T::Owned: Unpin,
{
}