Skip to content

Commit

Permalink
Fix all clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Luthaf committed Aug 29, 2023
1 parent 1b0a6fa commit 6aa4166
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 319 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chemfiles"
version = "0.10.4"
version = "0.10.40"
edition = "2021"
authors = ["Guillaume Fraux <guillaume.fraux@epfl.ch>"]
documentation = "http://chemfiles.org/chemfiles.rs/"
Expand All @@ -16,10 +16,10 @@ exclude = ["data/*"]
name = "chemfiles"

[dependencies]
chemfiles-sys = {path = "chemfiles-sys", version = "0.10.4"}
chemfiles-sys = {path = "chemfiles-sys", version = "0.10.40"}

[dev-dependencies]
approx = "0.4"
approx = "0.5"

[features]
# force a build from sources even if there is a matching pre-built version
Expand Down
2 changes: 1 addition & 1 deletion chemfiles-sys-tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(bad_style, unused)]
#![allow(warnings)]
use chemfiles_sys::*;
use libc::*;

Expand Down
59 changes: 32 additions & 27 deletions src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (C) 2015-2018 Guillaume Fraux -- BSD licensed
use std::marker::PhantomData;

use chemfiles_sys::*;
use chemfiles_sys as ffi;

use crate::errors::{check_not_null, check_success};
use crate::property::{PropertiesIter, Property, RawProperty};
Expand All @@ -21,7 +21,7 @@ use crate::strings;
/// `Ow`, `CH3`.
#[derive(Debug)]
pub struct Atom {
handle: *mut CHFL_ATOM,
handle: *mut ffi::CHFL_ATOM,
}

/// An analog to a reference to an atom (`&Atom`)
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<'a> std::ops::DerefMut for AtomMut<'a> {
impl Clone for Atom {
fn clone(&self) -> Atom {
unsafe {
let new_handle = chfl_atom_copy(self.as_ptr());
let new_handle = ffi::chfl_atom_copy(self.as_ptr());
Atom::from_ptr(new_handle)
}
}
Expand All @@ -73,7 +73,7 @@ impl Atom {
/// This function is unsafe because no validity check is made on the
/// pointer.
#[inline]
pub(crate) unsafe fn from_ptr(ptr: *mut CHFL_ATOM) -> Atom {
pub(crate) unsafe fn from_ptr(ptr: *mut ffi::CHFL_ATOM) -> Atom {
check_not_null(ptr);
Atom { handle: ptr }
}
Expand All @@ -83,9 +83,10 @@ impl Atom {
/// This function is unsafe because no validity check is made on the
/// pointer, and the caller is responsible for setting the right lifetime
#[inline]
pub(crate) unsafe fn ref_from_ptr<'a>(ptr: *const CHFL_ATOM) -> AtomRef<'a> {
#[allow(clippy::ptr_cast_constness)]
pub(crate) unsafe fn ref_from_ptr<'a>(ptr: *const ffi::CHFL_ATOM) -> AtomRef<'a> {
AtomRef {
inner: Atom::from_ptr(ptr as *mut CHFL_ATOM),
inner: Atom::from_ptr(ptr as *mut ffi::CHFL_ATOM),
marker: PhantomData,
}
}
Expand All @@ -95,7 +96,7 @@ impl Atom {
/// This function is unsafe because no validity check is made on the
/// pointer, and the caller is responsible for setting the right lifetime
#[inline]
pub(crate) unsafe fn ref_mut_from_ptr<'a>(ptr: *mut CHFL_ATOM) -> AtomMut<'a> {
pub(crate) unsafe fn ref_mut_from_ptr<'a>(ptr: *mut ffi::CHFL_ATOM) -> AtomMut<'a> {
AtomMut {
inner: Atom::from_ptr(ptr),
marker: PhantomData,
Expand All @@ -104,13 +105,13 @@ impl Atom {

/// Get the underlying C pointer as a const pointer.
#[inline]
pub(crate) fn as_ptr(&self) -> *const CHFL_ATOM {
pub(crate) fn as_ptr(&self) -> *const ffi::CHFL_ATOM {
self.handle
}

/// Get the underlying C pointer as a mutable pointer.
#[inline]
pub(crate) fn as_mut_ptr(&mut self) -> *mut CHFL_ATOM {
pub(crate) fn as_mut_ptr(&mut self) -> *mut ffi::CHFL_ATOM {
self.handle
}

Expand All @@ -125,7 +126,7 @@ impl Atom {
pub fn new<'a>(name: impl Into<&'a str>) -> Atom {
let buffer = strings::to_c(name.into());
unsafe {
let handle = chfl_atom(buffer.as_ptr());
let handle = ffi::chfl_atom(buffer.as_ptr());
Atom::from_ptr(handle)
}
}
Expand All @@ -141,7 +142,7 @@ impl Atom {
pub fn mass(&self) -> f64 {
let mut mass = 0.0;
unsafe {
check_success(chfl_atom_mass(self.as_ptr(), &mut mass));
check_success(ffi::chfl_atom_mass(self.as_ptr(), &mut mass));
}
return mass;
}
Expand All @@ -158,7 +159,7 @@ impl Atom {
/// ```
pub fn set_mass(&mut self, mass: f64) {
unsafe {
check_success(chfl_atom_set_mass(self.as_mut_ptr(), mass));
check_success(ffi::chfl_atom_set_mass(self.as_mut_ptr(), mass));
}
}

Expand All @@ -173,7 +174,7 @@ impl Atom {
pub fn charge(&self) -> f64 {
let mut charge = 0.0;
unsafe {
check_success(chfl_atom_charge(self.as_ptr(), &mut charge));
check_success(ffi::chfl_atom_charge(self.as_ptr(), &mut charge));
}
return charge;
}
Expand All @@ -190,7 +191,7 @@ impl Atom {
/// ```
pub fn set_charge(&mut self, charge: f64) {
unsafe {
check_success(chfl_atom_set_charge(self.as_mut_ptr(), charge));
check_success(ffi::chfl_atom_set_charge(self.as_mut_ptr(), charge));
}
}

Expand All @@ -203,7 +204,7 @@ impl Atom {
/// assert_eq!(atom.name(), "He");
/// ```
pub fn name(&self) -> String {
let get_name = |ptr, len| unsafe { chfl_atom_name(self.as_ptr(), ptr, len) };
let get_name = |ptr, len| unsafe { ffi::chfl_atom_name(self.as_ptr(), ptr, len) };
let name = strings::call_autogrow_buffer(10, get_name).expect("getting name failed");
return strings::from_c(name.as_ptr());
}
Expand All @@ -217,7 +218,7 @@ impl Atom {
/// assert_eq!(atom.atomic_type(), "He");
/// ```
pub fn atomic_type(&self) -> String {
let get_type = |ptr, len| unsafe { chfl_atom_type(self.as_ptr(), ptr, len) };
let get_type = |ptr, len| unsafe { ffi::chfl_atom_type(self.as_ptr(), ptr, len) };
let buffer = strings::call_autogrow_buffer(10, get_type).expect("getting type failed");
return strings::from_c(buffer.as_ptr());
}
Expand All @@ -235,7 +236,7 @@ impl Atom {
pub fn set_name<'a>(&mut self, name: impl Into<&'a str>) {
let buffer = strings::to_c(name.into());
unsafe {
check_success(chfl_atom_set_name(self.as_mut_ptr(), buffer.as_ptr()));
check_success(ffi::chfl_atom_set_name(self.as_mut_ptr(), buffer.as_ptr()));
}
}

Expand All @@ -252,7 +253,7 @@ impl Atom {
pub fn set_atomic_type<'a>(&mut self, atomic_type: impl Into<&'a str>) {
let buffer = strings::to_c(atomic_type.into());
unsafe {
check_success(chfl_atom_set_type(self.as_mut_ptr(), buffer.as_ptr()));
check_success(ffi::chfl_atom_set_type(self.as_mut_ptr(), buffer.as_ptr()));
}
}

Expand All @@ -267,7 +268,7 @@ impl Atom {
/// assert_eq!(atom.full_name(), "Zinc");
/// ```
pub fn full_name(&self) -> String {
let get_full_name = |ptr, len| unsafe { chfl_atom_full_name(self.as_ptr(), ptr, len) };
let get_full_name = |ptr, len| unsafe { ffi::chfl_atom_full_name(self.as_ptr(), ptr, len) };
let name = strings::call_autogrow_buffer(10, get_full_name).expect("getting full name failed");
return strings::from_c(name.as_ptr());
}
Expand All @@ -284,7 +285,7 @@ impl Atom {
pub fn vdw_radius(&self) -> f64 {
let mut radius: f64 = 0.0;
unsafe {
check_success(chfl_atom_vdw_radius(self.as_ptr(), &mut radius));
check_success(ffi::chfl_atom_vdw_radius(self.as_ptr(), &mut radius));
}
return radius;
}
Expand All @@ -301,7 +302,7 @@ impl Atom {
pub fn covalent_radius(&self) -> f64 {
let mut radius: f64 = 0.0;
unsafe {
check_success(chfl_atom_covalent_radius(self.as_ptr(), &mut radius));
check_success(ffi::chfl_atom_covalent_radius(self.as_ptr(), &mut radius));
}
return radius;
}
Expand All @@ -318,7 +319,7 @@ impl Atom {
pub fn atomic_number(&self) -> u64 {
let mut number = 0;
unsafe {
check_success(chfl_atom_atomic_number(self.as_ptr(), &mut number));
check_success(ffi::chfl_atom_atomic_number(self.as_ptr(), &mut number));
}
return number;
}
Expand All @@ -342,7 +343,7 @@ impl Atom {
let buffer = strings::to_c(name);
let property = property.into().as_raw();
unsafe {
check_success(chfl_atom_set_property(
check_success(ffi::chfl_atom_set_property(
self.as_mut_ptr(),
buffer.as_ptr(),
property.as_ptr(),
Expand All @@ -364,7 +365,7 @@ impl Atom {
pub fn get(&self, name: &str) -> Option<Property> {
let buffer = strings::to_c(name);
unsafe {
let handle = chfl_atom_get_property(self.as_ptr(), buffer.as_ptr());
let handle = ffi::chfl_atom_get_property(self.as_ptr(), buffer.as_ptr());
if handle.is_null() {
None
} else {
Expand Down Expand Up @@ -395,14 +396,18 @@ impl Atom {
pub fn properties(&self) -> PropertiesIter {
let mut count = 0;
unsafe {
check_success(chfl_atom_properties_count(self.as_ptr(), &mut count));
check_success(ffi::chfl_atom_properties_count(self.as_ptr(), &mut count));
}

#[allow(clippy::cast_possible_truncation)]
let size = count as usize;
let mut c_names = vec![std::ptr::null_mut(); size];
unsafe {
check_success(chfl_atom_list_properties(self.as_ptr(), c_names.as_mut_ptr(), count));
check_success(ffi::chfl_atom_list_properties(
self.as_ptr(),
c_names.as_mut_ptr(),
count,
));
}

let mut names = Vec::new();
Expand All @@ -420,7 +425,7 @@ impl Atom {
impl Drop for Atom {
fn drop(&mut self) {
unsafe {
let _ = chfl_free(self.as_ptr().cast());
let _ = ffi::chfl_free(self.as_ptr().cast());
}
}
}
Expand Down
Loading

0 comments on commit 6aa4166

Please sign in to comment.