Skip to content

Commit

Permalink
Merge pull request rusterlium#292 from rusterlium/rename-pid
Browse files Browse the repository at this point in the history
Rename Pid to LocalPid
  • Loading branch information
evnu authored Jan 28, 2020
2 parents 27c2160 + cd62676 commit 44ee0f3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changes

- Renamed `Pid` to `LocalPid` to clarify that it can't point to a remote process
- Dependencies have been updated.
- Derive macros have been refactored.
- Macros have been renamed and old ones have been deprecated:
Expand Down
10 changes: 5 additions & 5 deletions rustler/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::pid::Pid;
use crate::types::LocalPid;
use crate::wrapper::{NIF_ENV, NIF_TERM};
use crate::{Encoder, Term};
use std::marker::PhantomData;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a> Env<'a> {
/// Panics if the above rules are broken (by trying to send a message from
/// an `OwnedEnv` on a thread that's managed by the Erlang VM).
///
pub fn send(self, pid: &Pid, message: Term<'a>) {
pub fn send(self, pid: &LocalPid, message: Term<'a>) {
let thread_type = unsafe { rustler_sys::enif_thread_type() };
let env = if thread_type == rustler_sys::ERL_NIF_THR_UNDEFINED {
ptr::null_mut()
Expand Down Expand Up @@ -124,10 +124,10 @@ impl<'a> Env<'a> {
/// Erlang process.
///
/// use rustler::env::OwnedEnv;
/// use rustler::types::pid::Pid;
/// use rustler::types::LocalPid;
/// use rustler::Encoder;
///
/// fn send_string_to_pid(data: &str, pid: &Pid) {
/// fn send_string_to_pid(data: &str, pid: &LocalPid) {
/// let mut msg_env = OwnedEnv::new();
/// msg_env.send_and_clear(pid, |env| data.encode(env));
/// }
Expand Down Expand Up @@ -168,7 +168,7 @@ impl OwnedEnv {
/// can only use this method on a thread that was created by other
/// means. (This curious restriction is imposed by the Erlang VM.)
///
pub fn send_and_clear<F>(&mut self, recipient: &Pid, closure: F)
pub fn send_and_clear<F>(&mut self, recipient: &LocalPid, closure: F)
where
F: for<'a> FnOnce(Env<'a>) -> Term<'a>,
{
Expand Down
2 changes: 1 addition & 1 deletion rustler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod term;

pub use crate::term::Term;
pub use crate::types::{
Atom, Binary, Decoder, Encoder, ListIterator, MapIterator, OwnedBinary, Pid,
Atom, Binary, Decoder, Encoder, ListIterator, LocalPid, MapIterator, OwnedBinary,
};
pub mod resource;
pub use crate::resource::ResourceArc;
Expand Down
16 changes: 8 additions & 8 deletions rustler/src/types/pid.rs → rustler/src/types/local_pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ use crate::{Decoder, Encoder, Env, Error, NifResult, Term};
use std::mem::MaybeUninit;

#[derive(Clone)]
pub struct Pid {
pub struct LocalPid {
c: ErlNifPid,
}

impl Pid {
impl LocalPid {
pub fn as_c_arg(&self) -> &ErlNifPid {
&self.c
}
}

impl<'a> Decoder<'a> for Pid {
fn decode(term: Term<'a>) -> NifResult<Pid> {
impl<'a> Decoder<'a> for LocalPid {
fn decode(term: Term<'a>) -> NifResult<LocalPid> {
unsafe { pid::get_local_pid(term.get_env().as_c_arg(), term.as_c_arg()) }
.map(|pid| Pid { c: pid })
.map(|pid| LocalPid { c: pid })
.ok_or(Error::BadArg)
}
}

impl Encoder for Pid {
impl Encoder for LocalPid {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
unsafe { Term::new(env, pid::make_pid(env.as_c_arg(), self.c)) }
}
Expand All @@ -35,12 +35,12 @@ impl<'a> Env<'a> {
/// Panics if this environment is process-independent. (The only way to get such an
/// environment is to use `OwnedEnv`. The `Env` that Rustler passes to NIFs when they're
/// called is always associated with the calling Erlang process.)
pub fn pid(self) -> Pid {
pub fn pid(self) -> LocalPid {
let mut pid = MaybeUninit::uninit();
if unsafe { rustler_sys::enif_self(self.as_c_arg(), pid.as_mut_ptr()) }.is_null() {
panic!("environment is process-independent");
}
Pid {
LocalPid {
c: unsafe { pid.assume_init() },
}
}
Expand Down
14 changes: 11 additions & 3 deletions rustler/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use crate::types::list::ListIterator;

#[doc(hidden)]
pub mod map;
pub use crate::types::map::MapIterator;
pub use self::map::MapIterator;

#[doc(hidden)]
pub mod primitive;
Expand All @@ -23,8 +23,16 @@ pub mod string;
pub mod tuple;

#[doc(hidden)]
pub mod pid;
pub use crate::types::pid::Pid;
pub mod local_pid;
pub use self::local_pid::LocalPid;

#[deprecated(since = "0.22.0", note = "Please use local_pid instead")]
pub mod pid {
#[deprecated(since = "0.22.0", note = "Please use LocalPid instead")]
pub use super::LocalPid as Pid;
}
#[deprecated(since = "0.22.0", note = "Please use LocalPid instead")]
pub use self::LocalPid as Pid;

pub mod truthy;

Expand Down
4 changes: 2 additions & 2 deletions rustler_tests/native/rustler_test/src/test_env.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use rustler::env::{OwnedEnv, SavedTerm};
use rustler::types::atom;
use rustler::types::list::ListIterator;
use rustler::types::pid::Pid;
use rustler::types::LocalPid;
use rustler::{Atom, Encoder, Env, NifResult, Term};
use std::thread;

// Send a message to several PIDs.
#[rustler::nif]
pub fn send_all<'a>(env: Env<'a>, pids: Vec<Pid>, msg: Term<'a>) -> Term<'a> {
pub fn send_all<'a>(env: Env<'a>, pids: Vec<LocalPid>, msg: Term<'a>) -> Term<'a> {
for pid in pids {
env.send(&pid, msg);
}
Expand Down

0 comments on commit 44ee0f3

Please sign in to comment.