use core::borrow::Borrow;
use core::fmt;
use core::hash;
use core::ops::Deref;
use core::str::FromStr;
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;
use super::{ObjectPath, ObjectPathError, validate};
#[derive(Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct ObjectPathBuf(Vec<u8>);
impl ObjectPathBuf {
#[inline]
pub(super) unsafe fn from_raw_vec(data: Vec<u8>) -> Self {
Self(data)
}
#[inline]
fn to_object_path(&self) -> &ObjectPath {
unsafe { ObjectPath::new_unchecked(&self.0) }
}
}
impl TryFrom<Vec<u8>> for ObjectPathBuf {
type Error = ObjectPathError;
#[inline]
fn try_from(path: Vec<u8>) -> Result<Self, Self::Error> {
if !validate(&path) {
return Err(ObjectPathError);
}
Ok(Self(path))
}
}
impl TryFrom<String> for ObjectPathBuf {
type Error = ObjectPathError;
#[inline]
fn try_from(path: String) -> Result<Self, Self::Error> {
Self::try_from(path.into_bytes())
}
}
impl FromStr for ObjectPathBuf {
type Err = ObjectPathError;
#[inline]
fn from_str(path: &str) -> Result<Self, Self::Err> {
Ok(ObjectPath::new(path)?.to_owned())
}
}
impl hash::Hash for ObjectPathBuf {
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: hash::Hasher,
{
hash::Hash::hash(&**self, state);
}
}
impl fmt::Display for ObjectPathBuf {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
impl fmt::Debug for ObjectPathBuf {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl Deref for ObjectPathBuf {
type Target = ObjectPath;
#[inline]
fn deref(&self) -> &Self::Target {
self.to_object_path()
}
}
impl Borrow<ObjectPath> for ObjectPathBuf {
#[inline]
fn borrow(&self) -> &ObjectPath {
self
}
}
impl AsRef<ObjectPath> for ObjectPathBuf {
#[inline]
fn as_ref(&self) -> &ObjectPath {
self
}
}