musli_core/no_std.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
//! Trait fills for `#[no_std]` environments.
//!
//! * [`ToOwned`] - if the `alloc` feature is enabled, this is an alias for
//! `alloc::borrow::ToOwned`.
#[cfg(feature = "alloc")]
pub use rust_alloc::borrow::ToOwned;
#[cfg(not(feature = "alloc"))]
pub use self::to_owned::ToOwned;
#[cfg(not(feature = "alloc"))]
mod to_owned {
use core::borrow::Borrow;
/// Never type for [ToOwned] so that `Owned` can reference some type even if
/// it's uninhabitable.
pub enum NeverOwned {}
impl<T> Borrow<[T]> for NeverOwned
where
T: Clone,
{
fn borrow(&self) -> &[T] {
match *self {}
}
}
impl<T> Borrow<T> for NeverOwned
where
T: Clone,
{
fn borrow(&self) -> &T {
match *self {}
}
}
impl Borrow<str> for NeverOwned {
fn borrow(&self) -> &str {
match *self {}
}
}
/// Trait fill for ToOwned when we're in a `#[no_std]` environment.
pub trait ToOwned {
/// The value borrowed.
type Owned: Borrow<Self>;
}
impl<T> ToOwned for [T]
where
T: Clone,
{
type Owned = NeverOwned;
}
impl<T> ToOwned for T
where
T: Clone,
{
type Owned = NeverOwned;
}
impl ToOwned for str {
type Owned = NeverOwned;
}
}