Skip to content

Commit 8bb65fe

Browse files
authored
Merge branch 'master' into drop-deprecated
2 parents 3c214d9 + 730303e commit 8bb65fe

File tree

15 files changed

+109
-13
lines changed

15 files changed

+109
-13
lines changed

rustler/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ rust-version = "1.70"
1313
big_integer = ["dep:num-bigint"]
1414
default = ["nif_version_2_15"]
1515
derive = []
16-
alternative_nif_init_name = []
1716
allocator = []
1817
nif_version_2_14 = []
1918
nif_version_2_15 = ["nif_version_2_14"]

rustler/src/env.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct SendError;
4848

4949
impl<'a> Env<'a> {
5050
#[doc(hidden)]
51+
#[inline]
5152
pub(crate) unsafe fn new_internal<T>(
5253
_lifetime_marker: &'a T,
5354
env: NIF_ENV,
@@ -69,11 +70,13 @@ impl<'a> Env<'a> {
6970
///
7071
/// # Unsafe
7172
/// Don't create multiple `Env`s with the same lifetime.
73+
#[inline]
7274
pub unsafe fn new<T>(_lifetime_marker: &'a T, env: NIF_ENV) -> Env<'a> {
7375
Self::new_internal(_lifetime_marker, env, EnvKind::ProcessBound)
7476
}
7577

7678
#[doc(hidden)]
79+
#[inline]
7780
pub unsafe fn new_init_env<T>(_lifetime_marker: &'a T, env: NIF_ENV) -> Env<'a> {
7881
Self::new_internal(_lifetime_marker, env, EnvKind::Init)
7982
}
@@ -83,6 +86,7 @@ impl<'a> Env<'a> {
8386
}
8487

8588
/// Convenience method for building a tuple `{error, Reason}`.
89+
#[inline]
8690
pub fn error_tuple(self, reason: impl Encoder) -> Term<'a> {
8791
let error = crate::types::atom::error().to_term(self);
8892
(error, reason).encode(self)
@@ -101,6 +105,7 @@ impl<'a> Env<'a> {
101105
///
102106
/// The result indicates whether the send was successful, see also
103107
/// [enif\_send](https://www.erlang.org/doc/man/erl_nif.html#enif_send).
108+
#[inline]
104109
pub fn send(self, pid: &LocalPid, message: impl Encoder) -> Result<(), SendError> {
105110
let env = if is_scheduler_thread() {
106111
if self.kind == EnvKind::ProcessIndependent {

rustler/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod wrapper;
3030
pub mod codegen_runtime;
3131

3232
mod alloc;
33+
pub use crate::alloc::EnifAllocator;
3334

3435
#[macro_use]
3536
pub mod types;

rustler/src/serde/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn parse_bool(term: &Term) -> Result<bool, Error> {
2828
}
2929
}
3030

31-
pub fn parse_binary(term: Term) -> Result<&[u8], Error> {
31+
pub fn parse_binary<'a>(term: Term<'a>) -> Result<&'a [u8], Error> {
3232
validate_binary(&term)?;
3333
let binary: Binary = term.decode().or(Err(Error::ExpectedBinary))?;
3434
Ok(binary.as_slice())
@@ -42,7 +42,7 @@ pub fn parse_number<'a, T: Decoder<'a>>(term: &Term<'a>) -> Result<T, Error> {
4242
term.decode().or(Err(Error::ExpectedNumber))
4343
}
4444

45-
pub fn parse_str(term: Term) -> Result<&str, Error> {
45+
pub fn parse_str<'a>(term: Term<'a>) -> Result<&'a str, Error> {
4646
let bytes = parse_binary(term)?;
4747
std::str::from_utf8(bytes).or(Err(Error::ExpectedStringable))
4848
}

rustler/src/sys/nif_filler.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ pub(crate) trait DynNifFiller {
22
fn write<T: Copy>(&self, field: &mut Option<T>, name: &str);
33
}
44

5-
pub struct NullNifFiller;
6-
impl DynNifFiller for NullNifFiller {
7-
fn write<T: Copy>(&self, _field: &mut Option<T>, _name: &str) {}
8-
}
9-
105
#[cfg(not(target_os = "windows"))]
116
mod internal {
127
use std::ffi::OsStr;
@@ -43,6 +38,11 @@ mod internal {
4338
mod internal {
4439
use super::*;
4540

41+
pub struct NullNifFiller;
42+
impl DynNifFiller for NullNifFiller {
43+
fn write<T: Copy>(&self, _field: &mut Option<T>, _name: &str) {}
44+
}
45+
4646
pub fn new() -> impl DynNifFiller {
4747
NullNifFiller
4848
}

rustler/src/term.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ impl<'a> Term<'a> {
2929
/// # Unsafe
3030
/// The caller must ensure that `env` is the environment that `inner` belongs to,
3131
/// unless `inner` is an atom term.
32+
#[inline]
3233
pub unsafe fn new(env: Env<'a>, inner: NIF_TERM) -> Self {
3334
Term { term: inner, env }
3435
}
3536
/// This extracts the raw term pointer. It is usually used in order to obtain a type that can
3637
/// be passed to calls into the erlang vm.
38+
#[inline]
3739
pub fn as_c_arg(&self) -> NIF_TERM {
3840
self.term
3941
}
4042

43+
#[inline]
4144
pub fn get_env(self) -> Env<'a> {
4245
self.env
4346
}
@@ -46,6 +49,7 @@ impl<'a> Term<'a> {
4649
///
4750
/// If the term is already is in the provided env, it will be directly returned. Otherwise
4851
/// the term will be copied over.
52+
#[inline]
4953
pub fn in_env<'b>(&self, env: Env<'b>) -> Term<'b> {
5054
if self.get_env() == env {
5155
// It's safe to create a new Term<'b> without copying because we
@@ -81,13 +85,15 @@ impl<'a> Term<'a> {
8185
/// is needed.
8286
///
8387
/// [`decode`]: #method.decode
88+
#[inline]
8489
pub fn decode_as_binary(self) -> NifResult<Binary<'a>> {
8590
if self.is_binary() {
8691
return Binary::from_term(self);
8792
}
8893
Binary::from_iolist(self)
8994
}
9095

96+
#[inline]
9197
pub fn to_binary(self) -> OwnedBinary {
9298
let raw_binary = unsafe { term_to_binary(self.env.as_c_arg(), self.as_c_arg()) }.unwrap();
9399
unsafe { OwnedBinary::from_raw(raw_binary) }

rustler/src/types/binary.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ pub struct Binary<'a> {
248248

249249
impl<'a> Binary<'a> {
250250
/// Consumes `owned` and returns an immutable `Binary`.
251+
#[inline]
251252
pub fn from_owned(owned: OwnedBinary, env: Env<'a>) -> Self {
252253
// We are transferring ownership of `owned`'s data to the
253254
// environment. Therefore, we need to prevent `owned`'s destructor being
@@ -269,6 +270,7 @@ impl<'a> Binary<'a> {
269270
///
270271
/// If allocation fails, an error will be returned.
271272
#[allow(clippy::wrong_self_convention)]
273+
#[inline]
272274
pub fn to_owned(&self) -> Option<OwnedBinary> {
273275
OwnedBinary::from_unowned(self)
274276
}
@@ -278,6 +280,7 @@ impl<'a> Binary<'a> {
278280
/// # Errors
279281
///
280282
/// If `term` is not a binary, an error will be returned.
283+
#[inline]
281284
pub fn from_term(term: Term<'a>) -> Result<Self, Error> {
282285
let mut binary = MaybeUninit::uninit();
283286
if unsafe {
@@ -315,6 +318,7 @@ impl<'a> Binary<'a> {
315318
/// # Errors
316319
///
317320
/// If `term` is not an `iolist`, an error will be returned.
321+
#[inline]
318322
pub fn from_iolist(term: Term<'a>) -> Result<Self, Error> {
319323
let mut binary = MaybeUninit::uninit();
320324
if unsafe {
@@ -338,11 +342,13 @@ impl<'a> Binary<'a> {
338342

339343
/// Returns an Erlang term representation of `self`.
340344
#[allow(clippy::wrong_self_convention)]
345+
#[inline]
341346
pub fn to_term<'b>(&self, env: Env<'b>) -> Term<'b> {
342347
self.term.in_env(env)
343348
}
344349

345350
/// Extracts a slice containing the entire binary.
351+
#[inline]
346352
pub fn as_slice(&self) -> &'a [u8] {
347353
unsafe { ::std::slice::from_raw_parts(self.buf, self.size) }
348354
}
@@ -355,6 +361,7 @@ impl<'a> Binary<'a> {
355361
/// # Errors
356362
///
357363
/// If `offset + length` is out of bounds, an error will be returned.
364+
#[inline]
358365
pub fn make_subbinary(&self, offset: usize, length: usize) -> NifResult<Binary<'a>> {
359366
let min_len = length.checked_add(offset);
360367
if min_len.ok_or(Error::BadArg)? > self.size {
@@ -452,40 +459,47 @@ pub struct NewBinary<'a> {
452459

453460
impl<'a> NewBinary<'a> {
454461
/// Allocates a new `NewBinary`
462+
#[inline]
455463
pub fn new(env: Env<'a>, size: usize) -> Self {
456464
let (buf, term) = unsafe { new_binary(env, size) };
457465
NewBinary { buf, term, size }
458466
}
459467
/// Extracts a slice containing the entire binary.
468+
#[inline]
460469
pub fn as_slice(&self) -> &[u8] {
461470
unsafe { ::std::slice::from_raw_parts(self.buf, self.size) }
462471
}
463472

464473
/// Extracts a mutable slice of the entire binary.
474+
#[inline]
465475
pub fn as_mut_slice(&mut self) -> &mut [u8] {
466476
unsafe { ::std::slice::from_raw_parts_mut(self.buf, self.size) }
467477
}
468478
}
469479

470480
impl<'a> From<NewBinary<'a>> for Binary<'a> {
481+
#[inline]
471482
fn from(new_binary: NewBinary<'a>) -> Self {
472483
Binary::from_term(new_binary.term).unwrap()
473484
}
474485
}
475486

476487
impl<'a> From<NewBinary<'a>> for Term<'a> {
488+
#[inline]
477489
fn from(new_binary: NewBinary<'a>) -> Self {
478490
new_binary.term
479491
}
480492
}
481493

482494
impl Deref for NewBinary<'_> {
483495
type Target = [u8];
496+
#[inline]
484497
fn deref(&self) -> &[u8] {
485498
self.as_slice()
486499
}
487500
}
488501
impl DerefMut for NewBinary<'_> {
502+
#[inline]
489503
fn deref_mut(&mut self) -> &mut [u8] {
490504
self.as_mut_slice()
491505
}

rustler/src/types/list.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl<'a> ListIterator<'a> {
5656
impl<'a> Iterator for ListIterator<'a> {
5757
type Item = Term<'a>;
5858

59+
#[inline]
5960
fn next(&mut self) -> Option<Term<'a>> {
6061
let env = self.term.get_env();
6162
let cell = unsafe { list::get_list_cell(env.as_c_arg(), self.term.as_c_arg()) };
@@ -78,6 +79,7 @@ impl<'a> Iterator for ListIterator<'a> {
7879
}
7980

8081
impl<'a> Decoder<'a> for ListIterator<'a> {
82+
#[inline]
8183
fn decode(term: Term<'a>) -> NifResult<Self> {
8284
match ListIterator::new(term) {
8385
Some(iter) => Ok(iter),
@@ -96,6 +98,7 @@ impl<T> Encoder for Vec<T>
9698
where
9799
T: Encoder,
98100
{
101+
#[inline]
99102
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
100103
self.as_slice().encode(env)
101104
}
@@ -105,6 +108,7 @@ impl<'a, T> Decoder<'a> for Vec<T>
105108
where
106109
T: Decoder<'a>,
107110
{
111+
#[inline]
108112
fn decode(term: Term<'a>) -> NifResult<Self> {
109113
let iter: ListIterator = term.decode()?;
110114
let res: NifResult<Self> = iter.map(|x| x.decode::<T>()).collect();
@@ -116,6 +120,7 @@ impl<T> Encoder for [T]
116120
where
117121
T: Encoder,
118122
{
123+
#[inline]
119124
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
120125
let term_array: Vec<NIF_TERM> = self.iter().map(|x| x.encode(env).as_c_arg()).collect();
121126
unsafe { Term::new(env, list::make_list(env.as_c_arg(), &term_array)) }
@@ -126,6 +131,7 @@ impl<T> Encoder for &[T]
126131
where
127132
T: Encoder,
128133
{
134+
#[inline]
129135
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
130136
let term_array: Vec<NIF_TERM> = self.iter().map(|x| x.encode(env).as_c_arg()).collect();
131137
unsafe { Term::new(env, list::make_list(env.as_c_arg(), &term_array)) }
@@ -135,6 +141,7 @@ where
135141
/// ## List terms
136142
impl<'a> Term<'a> {
137143
/// Returns a new empty list.
144+
#[inline]
138145
pub fn list_new_empty(env: Env<'a>) -> Term<'a> {
139146
let list: &[u8] = &[];
140147
list.encode(env)
@@ -144,6 +151,7 @@ impl<'a> Term<'a> {
144151
/// See documentation for ListIterator for more information.
145152
///
146153
/// Returns None if the term is not a list.
154+
#[inline]
147155
pub fn into_list_iterator(self) -> NifResult<ListIterator<'a>> {
148156
ListIterator::new(self).ok_or(Error::BadArg)
149157
}
@@ -156,6 +164,7 @@ impl<'a> Term<'a> {
156164
/// ```elixir
157165
/// length(self_term)
158166
/// ```
167+
#[inline]
159168
pub fn list_length(self) -> NifResult<usize> {
160169
unsafe { list::get_list_length(self.get_env().as_c_arg(), self.as_c_arg()) }
161170
.ok_or(Error::BadArg)
@@ -171,6 +180,7 @@ impl<'a> Term<'a> {
171180
/// [head, tail] = self_term
172181
/// {head, tail}
173182
/// ```
183+
#[inline]
174184
pub fn list_get_cell(self) -> NifResult<(Term<'a>, Term<'a>)> {
175185
let env = self.get_env();
176186
unsafe {
@@ -183,6 +193,7 @@ impl<'a> Term<'a> {
183193
/// Makes a copy of the self list term and reverses it.
184194
///
185195
/// Returns Err(Error::BadArg) if the term is not a list.
196+
#[inline]
186197
pub fn list_reverse(self) -> NifResult<Term<'a>> {
187198
let env = self.get_env();
188199
unsafe {

rustler/src/types/local_pid.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ pub struct LocalPid {
1010
}
1111

1212
impl LocalPid {
13+
#[inline]
1314
pub fn as_c_arg(&self) -> &ErlNifPid {
1415
&self.c
1516
}
1617

18+
#[inline]
1719
pub fn from_c_arg(erl_nif_pid: ErlNifPid) -> Self {
1820
LocalPid { c: erl_nif_pid }
1921
}
@@ -25,6 +27,7 @@ impl LocalPid {
2527
}
2628

2729
impl<'a> Decoder<'a> for LocalPid {
30+
#[inline]
2831
fn decode(term: Term<'a>) -> NifResult<LocalPid> {
2932
unsafe { pid::get_local_pid(term.get_env().as_c_arg(), term.as_c_arg()) }
3033
.map(|pid| LocalPid { c: pid })
@@ -33,6 +36,7 @@ impl<'a> Decoder<'a> for LocalPid {
3336
}
3437

3538
impl Encoder for LocalPid {
39+
#[inline]
3640
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
3741
unsafe { Term::new(env, pid::make_pid(env.as_c_arg(), self.c)) }
3842
}
@@ -67,6 +71,7 @@ impl Env<'_> {
6771
/// Panics if this environment is process-independent. (The only way to get such an
6872
/// environment is to use `OwnedEnv`. The `Env` that Rustler passes to NIFs when they're
6973
/// called is always associated with the calling Erlang process.)
74+
#[inline]
7075
pub fn pid(self) -> LocalPid {
7176
let mut pid = MaybeUninit::uninit();
7277
if unsafe { enif_self(self.as_c_arg(), pid.as_mut_ptr()) }.is_null() {

0 commit comments

Comments
 (0)