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

Implement From<Runtime(Ref)> for Thread{Local,Safe} #616

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion rt/src/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use heph::{actor, sync, ActorRef, NewActor, Supervisor};
use crate::spawn::{ActorOptions, FutureOptions, Spawn};
use crate::timers::TimerToken;
use crate::trace::{self, Trace};
use crate::{shared, RuntimeRef};
use crate::{shared, Runtime, RuntimeRef};

/// Runtime Access Trait.
///
Expand Down Expand Up @@ -155,6 +155,12 @@ impl ThreadLocal {
}
}

impl From<RuntimeRef> for ThreadLocal {
fn from(rt: RuntimeRef) -> ThreadLocal {
ThreadLocal::new(rt)
}
}

impl Deref for ThreadLocal {
type Target = RuntimeRef;

Expand Down Expand Up @@ -286,6 +292,18 @@ impl ThreadSafe {
}
}

impl From<&Runtime> for ThreadSafe {
fn from(rt: &Runtime) -> ThreadSafe {
ThreadSafe::new(rt.internals.clone())
}
}

impl From<&RuntimeRef> for ThreadSafe {
fn from(rt: &RuntimeRef) -> ThreadSafe {
ThreadSafe::new(rt.clone_shared())
}
}

impl Access for ThreadSafe {}

impl PrivateAccess for ThreadSafe {
Expand Down
1 change: 0 additions & 1 deletion rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,6 @@ impl RuntimeRef {
}

/// Returns a copy of the shared internals.
#[cfg(any(test, feature = "test"))]
fn clone_shared(&self) -> Arc<shared::RuntimeInternals> {
self.internals.shared.clone()
}
Expand Down
1 change: 1 addition & 0 deletions rt/tests/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod util;

#[path = "functional"] // rustfmt can't find the files.
mod functional {
mod access;
mod actor;
mod actor_context;
mod actor_group;
Expand Down
53 changes: 53 additions & 0 deletions rt/tests/functional/access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Tests for the access module.

use heph_rt::{Runtime, RuntimeRef, ThreadLocal, ThreadSafe};

use crate::util::{assert_send, assert_sync};

#[test]
fn thread_safe_is_send_sync() {
assert_send::<ThreadSafe>();
assert_sync::<ThreadSafe>();
}

#[test]
fn thread_local_from_runtime_ref() {
let mut rt = Runtime::new().unwrap();
rt.run_on_workers(|runtime_ref| {
let thread_local = ThreadLocal::from(runtime_ref);
drop(thread_local);
Ok::<_, !>(())
})
.unwrap();
}

#[test]
fn thread_local_deref_as_runtime_ref() {
let mut rt = Runtime::new().unwrap();
rt.run_on_workers(|runtime_ref| {
let mut thread_local = ThreadLocal::from(runtime_ref);
let _runtime_ref: &mut RuntimeRef = &mut *thread_local;
drop(thread_local);
Ok::<_, !>(())
})
.unwrap();
}

#[test]
fn thread_safe_from_runtime() {
let rt = Runtime::new().unwrap();
let thread_safe = ThreadSafe::from(&rt);
drop(thread_safe);
drop(rt);
}

#[test]
fn thread_safe_from_runtime_ref() {
let mut rt = Runtime::new().unwrap();
rt.run_on_workers(|runtime_ref| {
let thread_safe = ThreadSafe::from(&runtime_ref);
drop(thread_safe);
Ok::<_, !>(())
})
.unwrap();
}
Loading