Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make types like Box<dyn DynAccess> implement Access #78

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 13 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Expand Up @@ -34,10 +34,13 @@ crossbeam-utils = "~0.8"
itertools = "0.10"
num_cpus = "~1"
once_cell = "~1"
parking_lot = "~0.12"
parking_lot = "~0.11"
proptest = "1"
serde_derive = "1.0.130"
serde_json = "1.0.69"
# to support MSRV in test:
bitflags = "=1.2.1"
half = "=1.7.1"

[profile.bench]
debug = true
Expand Down
42 changes: 41 additions & 1 deletion src/access.rs
Expand Up @@ -112,13 +112,53 @@ pub trait Access<T> {
fn load(&self) -> Self::Guard;
}

impl<T, A: Access<T>, P: Deref<Target = A>> Access<T> for P {
impl<T, A: Access<T> + ?Sized, P: Deref<Target = A>> Access<T> for P {
type Guard = A::Guard;
fn load(&self) -> Self::Guard {
self.deref().load()
}
}

impl<T> Access<T> for dyn DynAccess<T> + '_ {
type Guard = DynGuard<T>;

fn load(&self) -> Self::Guard {
self.load()
}
}

impl<T> Access<T> for dyn DynAccess<T> + '_ + Send {
type Guard = DynGuard<T>;

fn load(&self) -> Self::Guard {
self.load()
}
}

impl<T> Access<T> for dyn DynAccess<T> + '_ + Sync + Send {
type Guard = DynGuard<T>;

fn load(&self) -> Self::Guard {
self.load()
}
}

// Should probably be moved to a more appropriate place / perhaps more extensively tested / etc
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more appropriate place is just the end of the file (not the middle), otherwise it is fine :-)

#[cfg(test)]
mod test {
use super::*;
fn _expect_access<T>(_: impl Access<T>) {}
fn _test1<T>(x: Box<dyn DynAccess<T> + '_>) {
_expect_access(x)
}
fn _test2<T>(x: Box<dyn DynAccess<T> + '_ + Send>) {
_expect_access(x)
}
fn _test3<T>(x: Box<dyn DynAccess<T> + '_ + Send + Sync>) {
_expect_access(x)
}
}

impl<T: RefCnt, S: Strategy<T>> Access<T> for ArcSwapAny<T, S> {
type Guard = Guard<T, S>;

Expand Down
4 changes: 2 additions & 2 deletions src/as_raw.rs
Expand Up @@ -50,8 +50,8 @@ impl<'a, T: RefCnt> AsRaw<T::Base> for &'a Guard<T> {
}
}

impl<'a, T: RefCnt> Sealed for Guard<T> {}
impl<'a, T: RefCnt> AsRaw<T::Base> for Guard<T> {
impl<T: RefCnt> Sealed for Guard<T> {}
impl<T: RefCnt> AsRaw<T::Base> for Guard<T> {
fn as_raw(&self) -> *mut T::Base {
T::as_ptr(self)
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -164,7 +164,7 @@ pub struct Guard<T: RefCnt, S: Strategy<T> = DefaultStrategy> {
inner: S::Protected,
}

impl<'a, T: RefCnt, S: Strategy<T>> Guard<T, S> {
impl<T: RefCnt, S: Strategy<T>> Guard<T, S> {
/// Converts it into the held value.
///
/// This, on occasion, may be a tiny bit faster than cloning the Arc or whatever is being held
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<'a, T: RefCnt, S: Strategy<T>> Guard<T, S> {
}
}

impl<'a, T: RefCnt, S: Strategy<T>> Deref for Guard<T, S> {
impl<T: RefCnt, S: Strategy<T>> Deref for Guard<T, S> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
Expand Down