Skip to content

Commit bf1385a

Browse files
authored
Use impl Encoder in more places to simplify function usage (#572)
1 parent 88c6e15 commit bf1385a

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
See [`UPGRADE.md`](./UPGRADE.md) for additional help when upgrading to newer versions.
99

10+
## [unreleased]
11+
12+
### Changed
13+
14+
* Use `impl Encoder` on more functions (in particular on `send`) (#572)
15+
1016
## [0.30.0] - 2023-10-11
1117

1218
### Added

rustler/src/env.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ impl<'a> Env<'a> {
5656
}
5757

5858
/// Convenience method for building a tuple `{error, Reason}`.
59-
pub fn error_tuple<T>(self, reason: T) -> Term<'a>
60-
where
61-
T: Encoder,
62-
{
59+
pub fn error_tuple(self, reason: impl Encoder) -> Term<'a> {
6360
let error = crate::types::atom::error().to_term(self);
6461
(error, reason).encode(self)
6562
}
@@ -83,7 +80,7 @@ impl<'a> Env<'a> {
8380
/// Panics if the above rules are broken (by trying to send a message from
8481
/// an `OwnedEnv` on a thread that's managed by the Erlang VM).
8582
///
86-
pub fn send(self, pid: &LocalPid, message: Term<'a>) -> Result<(), SendError> {
83+
pub fn send(self, pid: &LocalPid, message: impl Encoder) -> Result<(), SendError> {
8784
let thread_type = unsafe { rustler_sys::enif_thread_type() };
8885
let env = if thread_type == rustler_sys::ERL_NIF_THR_UNDEFINED {
8986
ptr::null_mut()
@@ -99,6 +96,8 @@ impl<'a> Env<'a> {
9996
panic!("Env::send(): unrecognized calling thread type");
10097
};
10198

99+
let message = message.encode(self);
100+
102101
// Send the message.
103102
let res = unsafe {
104103
rustler_sys::enif_send(env, pid.as_c_arg(), ptr::null_mut(), message.as_c_arg())
@@ -120,7 +119,8 @@ impl<'a> Env<'a> {
120119
/// - `Some(pid)` if `name_or_pid` is an atom and an alive process is currently registered under the given name.
121120
/// - `None` if `name_or_pid` is an atom but there is no alive process registered under this name.
122121
/// - `None` if `name_or_pid` is not a PID or atom.
123-
pub fn whereis_pid(&self, name_or_pid: Term<'a>) -> Option<LocalPid> {
122+
pub fn whereis_pid(self, name_or_pid: impl Encoder) -> Option<LocalPid> {
123+
let name_or_pid = name_or_pid.encode(self);
124124
if name_or_pid.is_pid() {
125125
return Some(name_or_pid.decode().unwrap());
126126
}
@@ -198,9 +198,9 @@ impl OwnedEnv {
198198
}
199199

200200
/// Run some code in this environment.
201-
pub fn run<F, R>(&self, closure: F) -> R
201+
pub fn run<'a, F, R>(&self, closure: F) -> R
202202
where
203-
F: for<'a> FnOnce(Env<'a>) -> R,
203+
F: FnOnce(Env<'a>) -> R,
204204
{
205205
let env = unsafe { Env::new(&(), *self.env) };
206206
closure(env)
@@ -219,15 +219,20 @@ impl OwnedEnv {
219219
/// can only use this method on a thread that was created by other
220220
/// means. (This curious restriction is imposed by the Erlang VM.)
221221
///
222-
pub fn send_and_clear<F>(&mut self, recipient: &LocalPid, closure: F) -> Result<(), SendError>
222+
pub fn send_and_clear<'a, F, T>(
223+
&mut self,
224+
recipient: &LocalPid,
225+
closure: F,
226+
) -> Result<(), SendError>
223227
where
224-
F: for<'a> FnOnce(Env<'a>) -> Term<'a>,
228+
F: FnOnce(Env<'a>) -> T,
229+
T: Encoder,
225230
{
226231
if unsafe { rustler_sys::enif_thread_type() } != rustler_sys::ERL_NIF_THR_UNDEFINED {
227232
panic!("send_and_clear: current thread is managed");
228233
}
229234

230-
let message = self.run(|env| closure(env).as_c_arg());
235+
let message = self.run(|env| closure(env).encode(env).as_c_arg());
231236

232237
let res = unsafe {
233238
rustler_sys::enif_send(ptr::null_mut(), recipient.as_c_arg(), *self.env, message)
@@ -287,9 +292,9 @@ impl OwnedEnv {
287292
///
288293
/// **Note: There is no way to save terms across `OwnedEnv::send()` or `clear()`.**
289294
/// If you try, the `.load()` call will panic.
290-
pub fn save(&self, term: Term) -> SavedTerm {
295+
pub fn save(&self, term: impl Encoder) -> SavedTerm {
291296
SavedTerm {
292-
term: self.run(|env| term.in_env(env).as_c_arg()),
297+
term: self.run(|env| term.encode(env).as_c_arg()),
293298
env_generation: Arc::downgrade(&self.env),
294299
}
295300
}

rustler/src/term.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'a> Term<'a> {
3737
self.term
3838
}
3939

40-
pub fn get_env(&self) -> Env<'a> {
40+
pub fn get_env(self) -> Env<'a> {
4141
self.env
4242
}
4343

rustler/src/thread.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::env::OwnedEnv;
2-
use crate::types::atom::Atom;
3-
use crate::{Encoder, Env, Term};
2+
use crate::{Atom, Encoder, Env, Term};
43
use std::panic;
54
use std::thread;
65

rustler/src/types/list.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ impl<'a> Term<'a> {
193193
}
194194

195195
/// Adds `head` in a list cell with `self` as tail.
196-
pub fn list_prepend(self, head: Term<'a>) -> Term<'a> {
196+
pub fn list_prepend(self, head: impl Encoder) -> Term<'a> {
197197
let env = self.get_env();
198+
let head = head.encode(env);
198199
unsafe {
199200
let term = list::make_list_cell(env.as_c_arg(), head.as_c_arg(), self.as_c_arg());
200201
Term::new(env, term)

0 commit comments

Comments
 (0)