Skip to content

Commit

Permalink
Merge pull request #53 from GrigorenkoPV/as_ref
Browse files Browse the repository at this point in the history
Add some AsRef impls
  • Loading branch information
droundy authored Jul 19, 2024
2 parents 35faf06 + 9247935 commit e521f1c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ type Container<T> = DashMap<BoxRefCount<T>, (), RandomState>;
type Untyped = &'static (dyn Any + Send + Sync + 'static);
use std::borrow::Borrow;
use std::convert::AsRef;
use std::ffi::OsStr;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::path::Path;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;

Expand Down Expand Up @@ -304,6 +306,23 @@ impl<T: ?Sized + Send + Sync + Hash + Eq> AsRef<T> for ArcIntern<T> {
}
}

macro_rules! impl_as_ref {
($from:ty => $to:ty) => {
impl AsRef<$to> for ArcIntern<$from> {
#[inline(always)]
fn as_ref(&self) -> &$to {
let ptr: &$from = &*self;
ptr.as_ref()
}
}
};
}

impl_as_ref!(str => OsStr);
impl_as_ref!(str => Path);
impl_as_ref!(OsStr => Path);
impl_as_ref!(Path => OsStr);

impl<T: ?Sized + Eq + Hash + Send + Sync> Deref for ArcIntern<T> {
type Target = T;
fn deref(&self) -> &T {
Expand Down
18 changes: 18 additions & 0 deletions src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use super::boxedset;
use boxedset::HashSet;
use std::borrow::Borrow;
use std::convert::AsRef;
use std::ffi::OsStr;
use std::fmt::{Debug, Display, Pointer};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::path::Path;

use super::container;

Expand Down Expand Up @@ -324,6 +326,22 @@ impl<T: ?Sized> AsRef<T> for Intern<T> {
}
}

macro_rules! impl_as_ref {
($from:ty => $to:ty) => {
impl AsRef<$to> for Intern<$from> {
#[inline(always)]
fn as_ref(&self) -> &$to {
self.pointer.as_ref()
}
}
};
}

impl_as_ref!(str => OsStr);
impl_as_ref!(str => Path);
impl_as_ref!(OsStr => Path);
impl_as_ref!(Path => OsStr);

impl<T: Eq + Hash + Send + Sync + ?Sized> Deref for Intern<T> {
type Target = T;
fn deref(&self) -> &T {
Expand Down
18 changes: 18 additions & 0 deletions src/typearena.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![deny(missing_docs)]
use std::any::Any;
use std::any::TypeId;
use std::ffi::OsStr;
use std::hash::{Hash, Hasher};
use std::path::Path;

use append_only_vec::AppendOnlyVec;

Expand Down Expand Up @@ -368,6 +370,22 @@ impl<T: ?Sized> AsRef<T> for Intern<T> {
}
}

macro_rules! impl_as_ref {
($from:ty => $to:ty) => {
impl AsRef<$to> for Intern<$from> {
#[inline(always)]
fn as_ref(&self) -> &$to {
self.pointer.as_ref()
}
}
};
}

impl_as_ref!(str => OsStr);
impl_as_ref!(str => Path);
impl_as_ref!(OsStr => Path);
impl_as_ref!(Path => OsStr);

impl<T: Eq + Hash + Send + Sync + ?Sized> Deref for Intern<T> {
type Target = T;
fn deref(&self) -> &T {
Expand Down

0 comments on commit e521f1c

Please sign in to comment.