Skip to content

Commit 05f7974

Browse files
authored
Add inline (#475)
1 parent 977dccc commit 05f7974

File tree

10 files changed

+61
-0
lines changed

10 files changed

+61
-0
lines changed

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/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() {

rustler/src/types/map.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::wrapper::map;
55
use crate::{Decoder, Encoder, Env, Error, NifResult, Term};
66
use std::ops::RangeInclusive;
77

8+
#[inline]
89
pub fn map_new(env: Env) -> Term {
910
unsafe { Term::new(env, map::map_new(env.as_c_arg())) }
1011
}
@@ -17,6 +18,7 @@ impl<'a> Term<'a> {
1718
/// ```elixir
1819
/// %{}
1920
/// ```
21+
#[inline]
2022
pub fn map_new(env: Env<'a>) -> Term<'a> {
2123
map_new(env)
2224
}
@@ -29,6 +31,7 @@ impl<'a> Term<'a> {
2931
/// values = [1, 2]
3032
/// Enum.zip(keys, values) |> Map.new()
3133
/// ```
34+
#[inline]
3235
pub fn map_from_arrays(
3336
env: Env<'a>,
3437
keys: &[impl Encoder],
@@ -80,6 +83,7 @@ impl<'a> Term<'a> {
8083
/// ```elixir
8184
/// Map.new([{"foo", 1}, {"bar", 2}])
8285
/// ```
86+
#[inline]
8387
pub fn map_from_pairs(
8488
env: Env<'a>,
8589
pairs: &[(impl Encoder, impl Encoder)],
@@ -104,6 +108,7 @@ impl<'a> Term<'a> {
104108
/// ```elixir
105109
/// Map.get(self_term, key)
106110
/// ```
111+
#[inline]
107112
pub fn map_get(self, key: impl Encoder) -> NifResult<Term<'a>> {
108113
let env = self.get_env();
109114
match unsafe {
@@ -122,6 +127,7 @@ impl<'a> Term<'a> {
122127
/// ```elixir
123128
/// map_size(self_term)
124129
/// ```
130+
#[inline]
125131
pub fn map_size(self) -> NifResult<usize> {
126132
let env = self.get_env();
127133
unsafe { map::get_map_size(env.as_c_arg(), self.as_c_arg()).ok_or(Error::BadArg) }
@@ -136,6 +142,7 @@ impl<'a> Term<'a> {
136142
/// ```elixir
137143
/// Map.put(self_term, key, value)
138144
/// ```
145+
#[inline]
139146
pub fn map_put(self, key: impl Encoder, value: impl Encoder) -> NifResult<Term<'a>> {
140147
let env = self.get_env();
141148

@@ -161,6 +168,7 @@ impl<'a> Term<'a> {
161168
/// ```elixir
162169
/// Map.delete(self_term, key)
163170
/// ```
171+
#[inline]
164172
pub fn map_remove(self, key: impl Encoder) -> NifResult<Term<'a>> {
165173
let env = self.get_env();
166174

@@ -176,6 +184,7 @@ impl<'a> Term<'a> {
176184
///
177185
/// Returns Err(Error::BadArg) if the term is not a map of if key
178186
/// doesn't exist.
187+
#[inline]
179188
pub fn map_update(self, key: impl Encoder, new_value: impl Encoder) -> NifResult<Term<'a>> {
180189
let env = self.get_env();
181190

rustler/src/types/primitive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ macro_rules! erl_get {
2222
macro_rules! impl_number_encoder {
2323
($dec_type:ty, $nif_type:ty, $encode_fun:ident) => {
2424
impl Encoder for $dec_type {
25+
#[inline]
2526
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
2627
erl_make!(*self, env, $encode_fun, $nif_type)
2728
}
@@ -32,6 +33,7 @@ macro_rules! impl_number_encoder {
3233
macro_rules! impl_number_decoder {
3334
($dec_type:ty, $nif_type:ty, $decode_fun:ident) => {
3435
impl<'a> Decoder<'a> for $dec_type {
36+
#[inline]
3537
fn decode(term: Term) -> NifResult<$dec_type> {
3638
#![allow(unused_unsafe)]
3739
let mut res: $nif_type = Default::default();

0 commit comments

Comments
 (0)