@@ -56,10 +56,7 @@ impl<'a> Env<'a> {
56
56
}
57
57
58
58
/// 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 > {
63
60
let error = crate :: types:: atom:: error ( ) . to_term ( self ) ;
64
61
( error, reason) . encode ( self )
65
62
}
@@ -83,7 +80,7 @@ impl<'a> Env<'a> {
83
80
/// Panics if the above rules are broken (by trying to send a message from
84
81
/// an `OwnedEnv` on a thread that's managed by the Erlang VM).
85
82
///
86
- pub fn send ( self , pid : & LocalPid , message : Term < ' a > ) -> Result < ( ) , SendError > {
83
+ pub fn send ( self , pid : & LocalPid , message : impl Encoder ) -> Result < ( ) , SendError > {
87
84
let thread_type = unsafe { rustler_sys:: enif_thread_type ( ) } ;
88
85
let env = if thread_type == rustler_sys:: ERL_NIF_THR_UNDEFINED {
89
86
ptr:: null_mut ( )
@@ -99,6 +96,8 @@ impl<'a> Env<'a> {
99
96
panic ! ( "Env::send(): unrecognized calling thread type" ) ;
100
97
} ;
101
98
99
+ let message = message. encode ( self ) ;
100
+
102
101
// Send the message.
103
102
let res = unsafe {
104
103
rustler_sys:: enif_send ( env, pid. as_c_arg ( ) , ptr:: null_mut ( ) , message. as_c_arg ( ) )
@@ -120,7 +119,8 @@ impl<'a> Env<'a> {
120
119
/// - `Some(pid)` if `name_or_pid` is an atom and an alive process is currently registered under the given name.
121
120
/// - `None` if `name_or_pid` is an atom but there is no alive process registered under this name.
122
121
/// - `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 ) ;
124
124
if name_or_pid. is_pid ( ) {
125
125
return Some ( name_or_pid. decode ( ) . unwrap ( ) ) ;
126
126
}
@@ -198,9 +198,9 @@ impl OwnedEnv {
198
198
}
199
199
200
200
/// 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
202
202
where
203
- F : for < ' a > FnOnce ( Env < ' a > ) -> R ,
203
+ F : FnOnce ( Env < ' a > ) -> R ,
204
204
{
205
205
let env = unsafe { Env :: new ( & ( ) , * self . env ) } ;
206
206
closure ( env)
@@ -219,15 +219,20 @@ impl OwnedEnv {
219
219
/// can only use this method on a thread that was created by other
220
220
/// means. (This curious restriction is imposed by the Erlang VM.)
221
221
///
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 >
223
227
where
224
- F : for < ' a > FnOnce ( Env < ' a > ) -> Term < ' a > ,
228
+ F : FnOnce ( Env < ' a > ) -> T ,
229
+ T : Encoder ,
225
230
{
226
231
if unsafe { rustler_sys:: enif_thread_type ( ) } != rustler_sys:: ERL_NIF_THR_UNDEFINED {
227
232
panic ! ( "send_and_clear: current thread is managed" ) ;
228
233
}
229
234
230
- let message = self . run ( |env| closure ( env) . as_c_arg ( ) ) ;
235
+ let message = self . run ( |env| closure ( env) . encode ( env ) . as_c_arg ( ) ) ;
231
236
232
237
let res = unsafe {
233
238
rustler_sys:: enif_send ( ptr:: null_mut ( ) , recipient. as_c_arg ( ) , * self . env , message)
@@ -287,9 +292,9 @@ impl OwnedEnv {
287
292
///
288
293
/// **Note: There is no way to save terms across `OwnedEnv::send()` or `clear()`.**
289
294
/// 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 {
291
296
SavedTerm {
292
- term : self . run ( |env| term. in_env ( env) . as_c_arg ( ) ) ,
297
+ term : self . run ( |env| term. encode ( env) . as_c_arg ( ) ) ,
293
298
env_generation : Arc :: downgrade ( & self . env ) ,
294
299
}
295
300
}
0 commit comments