Skip to content

Commit 50d1528

Browse files
committed
Add inline
1 parent f946092 commit 50d1528

File tree

13 files changed

+78
-0
lines changed

13 files changed

+78
-0
lines changed

rustler/src/env.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@ impl<'a> Env<'a> {
4040
///
4141
/// # Unsafe
4242
/// Don't create multiple `Env`s with the same lifetime.
43+
#[inline]
4344
pub unsafe fn new<T>(_lifetime_marker: &'a T, env: NIF_ENV) -> Env<'a> {
4445
Env {
4546
env,
4647
id: PhantomData,
4748
}
4849
}
4950

51+
#[inline]
5052
pub fn as_c_arg(self) -> NIF_ENV {
5153
self.env
5254
}
5355

5456
/// Convenience method for building a tuple `{error, Reason}`.
57+
#[inline]
5558
pub fn error_tuple<T>(self, reason: T) -> Term<'a>
5659
where
5760
T: Encoder,
@@ -76,6 +79,7 @@ impl<'a> Env<'a> {
7679
/// Panics if the above rules are broken (by trying to send a message from
7780
/// an `OwnedEnv` on a thread that's managed by the Erlang VM).
7881
///
82+
#[inline]
7983
pub fn send(self, pid: &LocalPid, message: Term<'a>) {
8084
let thread_type = unsafe { rustler_sys::enif_thread_type() };
8185
let env = if thread_type == rustler_sys::ERL_NIF_THR_UNDEFINED {

rustler/src/term.rs

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

42+
#[inline]
4043
pub fn get_env(&self) -> Env<'a> {
4144
self.env
4245
}
@@ -45,6 +48,7 @@ impl<'a> Term<'a> {
4548
///
4649
/// If the term is already is in the provided env, it will be directly returned. Otherwise
4750
/// the term will be copied over.
51+
#[inline]
4852
pub fn in_env<'b>(&self, env: Env<'b>) -> Term<'b> {
4953
if self.get_env() == env {
5054
// It's safe to create a new Term<'b> without copying because we
@@ -85,13 +89,15 @@ impl<'a> Term<'a> {
8589
/// is needed.
8690
///
8791
/// [`decode`]: #method.decode
92+
#[inline]
8893
pub fn decode_as_binary(self) -> NifResult<Binary<'a>> {
8994
if self.is_binary() {
9095
return Binary::from_term(self);
9196
}
9297
Binary::from_iolist(self)
9398
}
9499

100+
#[inline]
95101
pub fn to_binary(self) -> OwnedBinary {
96102
let raw_binary = unsafe { term_to_binary(self.env.as_c_arg(), self.as_c_arg()) }.unwrap();
97103
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
@@ -242,6 +242,7 @@ pub struct Binary<'a> {
242242

243243
impl<'a> Binary<'a> {
244244
/// Consumes `owned` and returns an immutable `Binary`.
245+
#[inline]
245246
pub fn from_owned(owned: OwnedBinary, env: Env<'a>) -> Self {
246247
// We are transferring ownership of `owned`'s data to the
247248
// environment. Therefore, we need to prevent `owned`'s destructor being
@@ -267,6 +268,7 @@ impl<'a> Binary<'a> {
267268
///
268269
/// If allocation fails, an error will be returned.
269270
#[allow(clippy::wrong_self_convention)]
271+
#[inline]
270272
pub fn to_owned(&self) -> Option<OwnedBinary> {
271273
OwnedBinary::from_unowned(self)
272274
}
@@ -276,6 +278,7 @@ impl<'a> Binary<'a> {
276278
/// # Errors
277279
///
278280
/// If `term` is not a binary, an error will be returned.
281+
#[inline]
279282
pub fn from_term(term: Term<'a>) -> Result<Self, Error> {
280283
let mut binary = MaybeUninit::uninit();
281284
if unsafe {
@@ -299,6 +302,7 @@ impl<'a> Binary<'a> {
299302
/// # Errors
300303
///
301304
/// If `term` is not an `iolist`, an error will be returned.
305+
#[inline]
302306
pub fn from_iolist(term: Term<'a>) -> Result<Self, Error> {
303307
let mut binary = MaybeUninit::uninit();
304308
if unsafe {
@@ -319,11 +323,13 @@ impl<'a> Binary<'a> {
319323

320324
/// Returns an Erlang term representation of `self`.
321325
#[allow(clippy::wrong_self_convention)]
326+
#[inline]
322327
pub fn to_term<'b>(&self, env: Env<'b>) -> Term<'b> {
323328
self.term.in_env(env)
324329
}
325330

326331
/// Extracts a slice containing the entire binary.
332+
#[inline]
327333
pub fn as_slice(&self) -> &'a [u8] {
328334
unsafe { ::std::slice::from_raw_parts(self.inner.data, self.inner.size) }
329335
}
@@ -336,6 +342,7 @@ impl<'a> Binary<'a> {
336342
/// # Errors
337343
///
338344
/// If `offset + length` is out of bounds, an error will be returned.
345+
#[inline]
339346
pub fn make_subbinary(&self, offset: usize, length: usize) -> NifResult<Binary<'a>> {
340347
let min_len = length.checked_add(offset);
341348
if min_len.ok_or(Error::BadArg)? > self.inner.size {
@@ -415,40 +422,47 @@ pub struct NewBinary<'a> {
415422

416423
impl<'a> NewBinary<'a> {
417424
/// Allocates a new `NewBinary`
425+
#[inline]
418426
pub fn new(env: Env<'a>, size: usize) -> Self {
419427
let (buf, term) = unsafe { new_binary(env, size) };
420428
NewBinary { buf, term, size }
421429
}
422430
/// Extracts a slice containing the entire binary.
431+
#[inline]
423432
pub fn as_slice(&self) -> &[u8] {
424433
unsafe { ::std::slice::from_raw_parts(self.buf, self.size) }
425434
}
426435

427436
/// Extracts a mutable slice of the entire binary.
437+
#[inline]
428438
pub fn as_mut_slice(&mut self) -> &mut [u8] {
429439
unsafe { ::std::slice::from_raw_parts_mut(self.buf, self.size) }
430440
}
431441
}
432442

433443
impl<'a> From<NewBinary<'a>> for Binary<'a> {
444+
#[inline]
434445
fn from(new_binary: NewBinary<'a>) -> Self {
435446
Binary::from_term(new_binary.term).unwrap()
436447
}
437448
}
438449

439450
impl<'a> From<NewBinary<'a>> for Term<'a> {
451+
#[inline]
440452
fn from(new_binary: NewBinary<'a>) -> Self {
441453
new_binary.term
442454
}
443455
}
444456

445457
impl<'a> Deref for NewBinary<'a> {
446458
type Target = [u8];
459+
#[inline]
447460
fn deref(&self) -> &[u8] {
448461
self.as_slice()
449462
}
450463
}
451464
impl<'a> DerefMut for NewBinary<'a> {
465+
#[inline]
452466
fn deref_mut(&mut self) -> &mut [u8] {
453467
self.as_mut_slice()
454468
}

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),
@@ -97,6 +99,7 @@ impl<'a, T> Encoder for Vec<T>
9799
where
98100
T: Encoder,
99101
{
102+
#[inline]
100103
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
101104
self.as_slice().encode(env)
102105
}
@@ -106,6 +109,7 @@ impl<'a, T> Decoder<'a> for Vec<T>
106109
where
107110
T: Decoder<'a>,
108111
{
112+
#[inline]
109113
fn decode(term: Term<'a>) -> NifResult<Self> {
110114
let iter: ListIterator = term.decode()?;
111115
let res: NifResult<Self> = iter.map(|x| x.decode::<T>()).collect();
@@ -117,6 +121,7 @@ impl<'a, T> Encoder for [T]
117121
where
118122
T: Encoder,
119123
{
124+
#[inline]
120125
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
121126
let term_array: Vec<NIF_TERM> = self.iter().map(|x| x.encode(env).as_c_arg()).collect();
122127
unsafe { Term::new(env, list::make_list(env.as_c_arg(), &term_array)) }
@@ -126,6 +131,7 @@ impl<'a, T> Encoder for &'a [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
@@ -8,16 +8,19 @@ pub struct LocalPid {
88
}
99

1010
impl LocalPid {
11+
#[inline]
1112
pub fn as_c_arg(&self) -> &ErlNifPid {
1213
&self.c
1314
}
1415

16+
#[inline]
1517
pub fn from_c_arg(erl_nif_pid: ErlNifPid) -> Self {
1618
LocalPid { c: erl_nif_pid }
1719
}
1820
}
1921

2022
impl<'a> Decoder<'a> for LocalPid {
23+
#[inline]
2124
fn decode(term: Term<'a>) -> NifResult<LocalPid> {
2225
unsafe { pid::get_local_pid(term.get_env().as_c_arg(), term.as_c_arg()) }
2326
.map(|pid| LocalPid { c: pid })
@@ -26,6 +29,7 @@ impl<'a> Decoder<'a> for LocalPid {
2629
}
2730

2831
impl Encoder for LocalPid {
32+
#[inline]
2933
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
3034
unsafe { Term::new(env, pid::make_pid(env.as_c_arg(), self.c)) }
3135
}
@@ -39,6 +43,7 @@ impl<'a> Env<'a> {
3943
/// Panics if this environment is process-independent. (The only way to get such an
4044
/// environment is to use `OwnedEnv`. The `Env` that Rustler passes to NIFs when they're
4145
/// called is always associated with the calling Erlang process.)
46+
#[inline]
4247
pub fn pid(self) -> LocalPid {
4348
let mut pid = MaybeUninit::uninit();
4449
if unsafe { rustler_sys::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
/// List.zip(keys, values) |> Map.new()
3133
/// ```
34+
#[inline]
3235
pub fn map_from_arrays(
3336
env: Env<'a>,
3437
keys: &[impl Encoder],
@@ -57,6 +60,7 @@ impl<'a> Term<'a> {
5760
/// ```elixir
5861
/// Map.new([{"foo", 1}, {"bar", 2}])
5962
/// ```
63+
#[inline]
6064
pub fn map_from_pairs(
6165
env: Env<'a>,
6266
pairs: &[(impl Encoder, impl Encoder)],
@@ -81,6 +85,7 @@ impl<'a> Term<'a> {
8185
/// ```elixir
8286
/// Map.get(self_term, key)
8387
/// ```
88+
#[inline]
8489
pub fn map_get(self, key: impl Encoder) -> NifResult<Term<'a>> {
8590
let env = self.get_env();
8691
match unsafe {
@@ -99,6 +104,7 @@ impl<'a> Term<'a> {
99104
/// ```elixir
100105
/// map_size(self_term)
101106
/// ```
107+
#[inline]
102108
pub fn map_size(self) -> NifResult<usize> {
103109
let env = self.get_env();
104110
unsafe { map::get_map_size(env.as_c_arg(), self.as_c_arg()).ok_or(Error::BadArg) }
@@ -113,6 +119,7 @@ impl<'a> Term<'a> {
113119
/// ```elixir
114120
/// Map.put(self_term, key, value)
115121
/// ```
122+
#[inline]
116123
pub fn map_put(self, key: impl Encoder, value: impl Encoder) -> NifResult<Term<'a>> {
117124
let env = self.get_env();
118125

@@ -138,6 +145,7 @@ impl<'a> Term<'a> {
138145
/// ```elixir
139146
/// Map.delete(self_term, key)
140147
/// ```
148+
#[inline]
141149
pub fn map_remove(self, key: impl Encoder) -> NifResult<Term<'a>> {
142150
let env = self.get_env();
143151

@@ -153,6 +161,7 @@ impl<'a> Term<'a> {
153161
///
154162
/// Returns Err(Error::BadArg) if the term is not a map of if key
155163
/// doesn't exist.
164+
#[inline]
156165
pub fn map_update(self, key: impl Encoder, new_value: impl Encoder) -> NifResult<Term<'a>> {
157166
let env = self.get_env();
158167

rustler/src/types/primitive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{Decoder, Encoder, Env, Error, NifResult, Term};
44
macro_rules! impl_number_transcoder {
55
($dec_type:ty, $nif_type:ty, $encode_fun:ident, $decode_fun:ident) => {
66
impl Encoder for $dec_type {
7+
#[inline]
78
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
89
#[allow(clippy::cast_lossless)]
910
unsafe {
@@ -15,6 +16,7 @@ macro_rules! impl_number_transcoder {
1516
}
1617
}
1718
impl<'a> Decoder<'a> for $dec_type {
19+
#[inline]
1820
fn decode(term: Term) -> NifResult<$dec_type> {
1921
#![allow(unused_unsafe)]
2022
let mut res: $nif_type = Default::default();

0 commit comments

Comments
 (0)