Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy lints #671

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rustler/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn write_variadic_fn_type(out: &mut String, args: &str, ret: &str) {
}

pub struct CallbacksApiBuilder<'a>(&'a mut String);
impl<'a> ApiBuilder for CallbacksApiBuilder<'a> {
impl ApiBuilder for CallbacksApiBuilder<'_> {
fn init(&mut self) {
writeln!(self.0, "#[allow(dead_code)]").unwrap();
writeln!(self.0, "#[derive(Default, Copy, Clone)]").unwrap();
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<'a> ApiBuilder for CallbacksApiBuilder<'a> {
}

pub struct ForwardersApiBuilder<'a>(&'a mut String);
impl<'a> ApiBuilder for ForwardersApiBuilder<'a> {
impl ApiBuilder for ForwardersApiBuilder<'_> {
fn func(&mut self, ret: &str, name: &str, args: &str) {
// This regex takes a list of args with types and return only the name of the args.
//
Expand Down Expand Up @@ -135,7 +135,7 @@ impl<'a> ApiBuilder for ForwardersApiBuilder<'a> {

pub struct WriterBuilder<'a>(&'a mut String);

impl<'a> ApiBuilder for WriterBuilder<'a> {
impl ApiBuilder for WriterBuilder<'_> {
fn init(&mut self) {
write!(
self.0,
Expand Down
2 changes: 1 addition & 1 deletion rustler/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ macro_rules! impl_check {
}

/// ## Type checks
impl<'a> Term<'a> {
impl Term<'_> {
/// Returns an enum representing which type the term is.
/// Note that using the individual `is_*` functions is more
/// efficient for checking a single type.
Expand Down
2 changes: 1 addition & 1 deletion rustler/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Env<'a> {
/// Two environments are equal if they're the same `NIF_ENV` value.
///
/// A `Env<'a>` is equal to a `Env<'b>` if and only if `'a` and `'b` are the same lifetime.
impl<'a, 'b> PartialEq<Env<'b>> for Env<'a> {
impl<'b> PartialEq<Env<'b>> for Env<'_> {
fn eq(&self, other: &Env<'b>) -> bool {
self.env == other.env
}
Expand Down
2 changes: 1 addition & 1 deletion rustler/src/resource/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ unsafe impl Sync for Registration {}

inventory::collect!(Registration);

impl<'a> Env<'a> {
impl Env<'_> {
/// Register a resource type, see `Registration::register`.
pub fn register<T: Resource>(&self) -> Result<(), ResourceInitError> {
Registration::new::<T>().register(*self)
Expand Down
2 changes: 2 additions & 0 deletions rustler/src/resource/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type NifResourcePtr = *const ErlNifResourceType;
static mut RESOURCE_TYPES: OnceLock<HashMap<TypeId, usize>> = OnceLock::new();

/// Register an Erlang resource type handle for a particular type given by its `TypeId`
#[allow(static_mut_refs)]
pub(crate) unsafe fn register_resource_type(type_id: TypeId, resource_type: NifResourcePtr) {
RESOURCE_TYPES.get_or_init(Default::default);
RESOURCE_TYPES
Expand Down Expand Up @@ -64,6 +65,7 @@ pub trait Resource: Sized + Send + Sync + 'static {
#[doc(hidden)]
pub(crate) trait ResourceExt: 'static {
/// Get the NIF resource type handle for this type if it had been registered before
#[allow(static_mut_refs)]
fn get_resource_type() -> Option<NifResourcePtr> {
let map = unsafe { RESOURCE_TYPES.get()? };
map.get(&TypeId::of::<Self>())
Expand Down
2 changes: 1 addition & 1 deletion rustler/src/return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum Return<'a> {
Error(Error),
}

unsafe impl<'b> NifReturnable for Return<'b> {
unsafe impl NifReturnable for Return<'_> {
unsafe fn into_returned(self, env: Env) -> NifReturned {
match self {
Return::Term(inner) => NifReturned::Term(inner.as_c_arg()),
Expand Down
1 change: 1 addition & 0 deletions rustler/src/sys/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub unsafe fn internal_set_symbols(callbacks: DynNifCallbacks) {
DYN_NIF_CALLBACKS = callbacks;
}

#[allow(static_mut_refs)]
pub unsafe fn internal_write_symbols() {
let filler = nif_filler::new();
DYN_NIF_CALLBACKS.write_symbols(filler);
Expand Down
14 changes: 7 additions & 7 deletions rustler/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Term<'a> {
env: Env<'a>,
}

impl<'a> Debug for Term<'a> {
impl Debug for Term<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
crate::wrapper::term::fmt(self.as_c_arg(), f)
}
Expand Down Expand Up @@ -121,12 +121,12 @@ impl<'a> Term<'a> {
}
}

impl<'a> PartialEq for Term<'a> {
impl PartialEq for Term<'_> {
fn eq(&self, other: &Term) -> bool {
unsafe { enif_is_identical(self.as_c_arg(), other.as_c_arg()) == 1 }
}
}
impl<'a> Eq for Term<'a> {}
impl Eq for Term<'_> {}

fn cmp(lhs: &Term, rhs: &Term) -> Ordering {
let ord = unsafe { enif_compare(lhs.as_c_arg(), rhs.as_c_arg()) };
Expand All @@ -137,7 +137,7 @@ fn cmp(lhs: &Term, rhs: &Term) -> Ordering {
}
}

impl<'a> Ord for Term<'a> {
impl Ord for Term<'_> {
fn cmp(&self, other: &Term) -> Ordering {
cmp(self, other)
}
Expand All @@ -148,7 +148,7 @@ impl<'a> PartialOrd for Term<'a> {
}
}

impl<'a> Hash for Term<'a> {
impl Hash for Term<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
// As far as I can see, there is really no way
// to get a seed from the hasher. This is definitely
Expand All @@ -157,5 +157,5 @@ impl<'a> Hash for Term<'a> {
}
}

unsafe impl<'a> Sync for Term<'a> {}
unsafe impl<'a> Send for Term<'a> {}
unsafe impl Sync for Term<'_> {}
unsafe impl Send for Term<'_> {}
2 changes: 1 addition & 1 deletion rustler/src/types/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<'a> PartialEq<Term<'a>> for Atom {
}

/// ## Atom terms
impl<'a> Term<'a> {
impl Term<'_> {
/// When the term is an atom, this method will return the string
/// representation of it.
///
Expand Down
29 changes: 14 additions & 15 deletions rustler/src/types/big_int.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
use crate::{Decoder, Encoder, Env, Error, NifResult, Term};

use num_bigint::{BigInt, Sign};

// From https://www.erlang.org/doc/apps/erts/erl_ext_dist.html
const EXTERNAL_TERM_FORMAT_VERSION: u8 = 131;
const SMALL_INTEGER: u8 = 97;
const INTEGER: u8 = 98;
const SMALL_BIG_EXT: u8 = 110;
const LARGE_BIG_EXT: u8 = 111;

crate::atoms! {
big_int_encoder_invalid_bytes
}

/// Implementation of [Decoder](rustler::Decoder) and [Encoder](rustler::Encoder) traits for
/// num-bigint.
///
Expand Down Expand Up @@ -48,6 +33,20 @@ crate::atoms! {
/// }
/// ```
///
use crate::{Decoder, Encoder, Env, Error, NifResult, Term};

use num_bigint::{BigInt, Sign};

// From https://www.erlang.org/doc/apps/erts/erl_ext_dist.html
const EXTERNAL_TERM_FORMAT_VERSION: u8 = 131;
const SMALL_INTEGER: u8 = 97;
const INTEGER: u8 = 98;
const SMALL_BIG_EXT: u8 = 110;
const LARGE_BIG_EXT: u8 = 111;

crate::atoms! {
big_int_encoder_invalid_bytes
}

fn decode_big_integer(input: &[u8]) -> NifResult<BigInt> {
if Some(&EXTERNAL_TERM_FORMAT_VERSION) != input.first() {
Expand Down
10 changes: 5 additions & 5 deletions rustler/src/types/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,12 @@ impl<'a> Binary<'a> {
}
}

impl<'a> Borrow<[u8]> for Binary<'a> {
impl Borrow<[u8]> for Binary<'_> {
fn borrow(&self) -> &[u8] {
self.as_slice()
}
}
impl<'a> Deref for Binary<'a> {
impl Deref for Binary<'_> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.as_slice()
Expand All @@ -410,7 +410,7 @@ impl<'a> Decoder<'a> for Binary<'a> {
Binary::from_term(term)
}
}
impl<'a> Encoder for Binary<'a> {
impl Encoder for Binary<'_> {
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
self.to_term(env)
}
Expand Down Expand Up @@ -479,13 +479,13 @@ impl<'a> From<NewBinary<'a>> for Term<'a> {
}
}

impl<'a> Deref for NewBinary<'a> {
impl Deref for NewBinary<'_> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.as_slice()
}
}
impl<'a> DerefMut for NewBinary<'a> {
impl DerefMut for NewBinary<'_> {
fn deref_mut(&mut self) -> &mut [u8] {
self.as_mut_slice()
}
Expand Down
2 changes: 1 addition & 1 deletion rustler/src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where
}
}

impl<'a, T> Encoder for &'a [T]
impl<T> Encoder for &[T]
where
T: Encoder,
{
Expand Down
2 changes: 1 addition & 1 deletion rustler/src/types/local_pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Ord for LocalPid {
}
}

impl<'a> Env<'a> {
impl Env<'_> {
/// Return the calling process's pid.
///
/// # Panics
Expand Down
4 changes: 2 additions & 2 deletions rustler/src/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl<'a> SimpleMapIterator<'a> {
}
}

impl<'a> Drop for SimpleMapIterator<'a> {
impl Drop for SimpleMapIterator<'_> {
fn drop(&mut self) {
if let Some(iter) = self.iter.as_mut() {
unsafe {
Expand Down Expand Up @@ -314,7 +314,7 @@ impl<'a> Iterator for MapIterator<'a> {
}
}

impl<'a> DoubleEndedIterator for MapIterator<'a> {
impl DoubleEndedIterator for MapIterator<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.reverse.next().and_then(|(key, value)| {
if self.forward.last_key == Some(key) {
Expand Down
4 changes: 2 additions & 2 deletions rustler/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub trait Decoder<'a>: Sized + 'a {
fn decode(term: Term<'a>) -> NifResult<Self>;
}

impl<'a> Encoder for Term<'a> {
impl Encoder for Term<'_> {
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
self.in_env(env)
}
Expand All @@ -62,7 +62,7 @@ impl<'a> Decoder<'a> for Term<'a> {
}
}

impl<'a, T> Encoder for &'a T
impl<T> Encoder for &T
where
T: Encoder,
{
Expand Down
4 changes: 2 additions & 2 deletions rustler/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl_number_transcoder!(isize, i64, enif_make_int64, enif_get_int64);
impl_number_encoder!(f32, f64, enif_make_double);

// Manual Decoder impls for floats so they can fall back to decoding from integer terms
impl<'a> Decoder<'a> for f64 {
impl Decoder<'_> for f64 {
fn decode(term: Term) -> NifResult<f64> {
#![allow(unused_unsafe)]
let mut res: f64 = Default::default();
Expand All @@ -80,7 +80,7 @@ impl<'a> Decoder<'a> for f64 {
}
}

impl<'a> Decoder<'a> for f32 {
impl Decoder<'_> for f32 {
fn decode(term: Term) -> NifResult<f32> {
let res: f64 = term.decode()?;
let res = res as f32;
Expand Down
4 changes: 2 additions & 2 deletions rustler/src/types/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::sys::enif_make_ref;
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Reference<'a>(Term<'a>);

impl<'a> Reference<'a> {
impl Reference<'_> {
/// Returns a representation of self in the given Env.
///
/// If the term is already is in the provided env, it will be directly returned. Otherwise
Expand Down Expand Up @@ -50,7 +50,7 @@ impl<'a> Decoder<'a> for Reference<'a> {
}
}

impl<'a> Encoder for Reference<'a> {
impl Encoder for Reference<'_> {
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
self.0.encode(env)
}
Expand Down
2 changes: 1 addition & 1 deletion rustler/src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'a> Decoder<'a> for &'a str {

use std::io::Write;

impl<'a> Encoder for &'a str {
impl Encoder for &str {
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
(*self).encode(env)
}
Expand Down
9 changes: 0 additions & 9 deletions rustler/src/types/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use crate::wrapper::{tuple, NIF_TERM};
use crate::{Decoder, Encoder, Env, Error, NifResult, Term};

/// ## Tuple terms
//impl<'a> Term<'a> {
//
// pub fn tuple_to_vec(self) -> NifResult<Term<'a>> {
//
// }
//
//}

/// Convert an Erlang tuple to a Rust vector. (To convert to a Rust tuple, use `term.decode()`
/// instead.)
///
Expand Down
1 change: 0 additions & 1 deletion rustler_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ pub fn nif(args: TokenStream, input: TokenStream) -> TokenStream {
///
/// Then the traits `Encoder` and `Decoder` are derived automatically for your Rust struct
/// such that you can use the Elixir struct definition for it.

#[proc_macro_derive(NifStruct, attributes(module, rustler))]
pub fn nif_struct(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
Expand Down
Loading