1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2025-01-03 18:16:22 +01:00

Improved with clippy

This commit is contained in:
Anthony Scemama 2023-10-18 15:51:00 +02:00
parent 44a03f5551
commit 8a2ded80bc
4 changed files with 22 additions and 18 deletions

View File

@ -2,7 +2,7 @@ default: src/generated.rs
cargo build cargo build
cargo test cargo test
generated.rs: build.py src/generated.rs: build.py
python3 build.py python3 build.py
test: default test: default

View File

@ -88,7 +88,11 @@ use std::ffi::CString;
/// This implementation block includes additional functions automatically generated from tables. /// This implementation block includes additional functions automatically generated from tables.
/// For more details, refer to [TREXIO tables documentation](https://trex-coe.github.io/trexio/trex.html). /// For more details, refer to [TREXIO tables documentation](https://trex-coe.github.io/trexio/trex.html).
impl File {""" ] impl File {
#![allow(clippy::unnecessary_cast)]
#![allow(clippy::useless_conversion)]
#![allow(clippy::type_complexity)]
""" ]
for group in data: for group in data:
group_l = group.lower() group_l = group.lower()
@ -407,12 +411,12 @@ f"/// let two_d_array: Vec<_> = one_d_array.chunks({dim_group}_{dim_element}).co
let mut dset_out: Vec<*mut i8> = vec![std::ptr::null_mut(); size]; let mut dset_out: Vec<*mut i8> = vec![std::ptr::null_mut(); size];
// Allocate C-style strings and populate dset_out // Allocate C-style strings and populate dset_out
for i in 0..size{ for item in dset_out.iter_mut().take(size){
let c_str: *mut i8 = unsafe { std::alloc::alloc_zeroed(std::alloc::Layout::array::<i8>(capacity).unwrap()) as *mut i8 }; let c_str: *mut i8 = unsafe { std::alloc::alloc_zeroed(std::alloc::Layout::array::<i8>(capacity).unwrap()) as *mut i8 };
if c_str.is_null() { if c_str.is_null() {
return Err(ExitCode::AllocationFailed); return Err(ExitCode::AllocationFailed);
} }
dset_out[i] = c_str; *item = c_str;
} }
@ -461,11 +465,11 @@ f"/// let two_d_array: Vec<_> = one_d_array.chunks({dim_group}_{dim_element}).co
pub fn write_{group_l}_{element_l}(&self, data: &[&str]) -> Result<(), ExitCode> { pub fn write_{group_l}_{element_l}(&self, data: &[&str]) -> Result<(), ExitCode> {
let mut size = 0; let mut size = 0;
// Find longest string // Find longest string
for s in data.iter() { for s in data {
let l = s.len(); let l = s.len();
size = if l>size {l} else {size}; size = if l>size {l} else {size};
} }
size = size+1; size += 1;
let data_c : Vec<CString> = data.iter().map(|&x| string_to_c(x)).collect::<Vec<_>>(); let data_c : Vec<CString> = data.iter().map(|&x| string_to_c(x)).collect::<Vec<_>>();
let data_c : Vec<*const c_char> = data_c.iter().map(|x| x.as_ptr() as *const c_char).collect::<Vec<_>>(); let data_c : Vec<*const c_char> = data_c.iter().map(|x| x.as_ptr() as *const c_char).collect::<Vec<_>>();
let size : i32 = size.try_into().expect("try_into failed in write_{group}_{element} (size)"); let size : i32 = size.try_into().expect("try_into failed in write_{group}_{element} (size)");
@ -557,7 +561,7 @@ pub fn write_{group_l}_{element_l}(&self, offset: usize, data: &[{typ}]) -> Resu
let mut idx = Vec::<i32>::with_capacity({size}*data.len()); let mut idx = Vec::<i32>::with_capacity({size}*data.len());
let mut val = Vec::<f64>::with_capacity(data.len()); let mut val = Vec::<f64>::with_capacity(data.len());
for d in data.iter() { for d in data {
""" + """ +
'\n'.join([ f" idx.push(d.{i}.try_into().unwrap());" for i in range(size) ]) + '\n'.join([ f" idx.push(d.{i}.try_into().unwrap());" for i in range(size) ]) +
f"\n val.push(d.{size});" + f"\n val.push(d.{size});" +

View File

@ -15,7 +15,7 @@ impl Bitfield {
.len() .len()
.try_into() .try_into()
.expect("try_into failed in Bitfield::from"); .expect("try_into failed in Bitfield::from");
let orb_list_ptr = orb_list.as_ptr() as *const i32; let orb_list_ptr: *const i32 = orb_list.as_ptr();
let n_int32: i32 = n_int.try_into().expect("try_into failed in Bitfield::from"); let n_int32: i32 = n_int.try_into().expect("try_into failed in Bitfield::from");
let mut b = vec![0i64; n_int]; let mut b = vec![0i64; n_int];
let bit_list = b.as_mut_ptr() as *mut c::bitfield_t; let bit_list = b.as_mut_ptr() as *mut c::bitfield_t;
@ -50,7 +50,7 @@ impl Bitfield {
pub fn alpha(&self) -> Bitfield { pub fn alpha(&self) -> Bitfield {
let n_int = self.data.len() / 2; let n_int = self.data.len() / 2;
Bitfield { Bitfield {
data: (&self.data[0..n_int]).to_vec(), data: (self.data[0..n_int]).to_vec(),
} }
} }
@ -58,7 +58,7 @@ impl Bitfield {
pub fn beta(&self) -> Bitfield { pub fn beta(&self) -> Bitfield {
let n_int = self.data.len() / 2; let n_int = self.data.len() / 2;
Bitfield { Bitfield {
data: (&self.data[n_int..2 * n_int]).to_vec(), data: (self.data[n_int..2 * n_int]).to_vec(),
} }
} }
@ -86,7 +86,7 @@ impl Bitfield {
let d1 = self.as_ptr(); let d1 = self.as_ptr();
let cap = self.data.len() * 64; let cap = self.data.len() * 64;
let mut list = vec![0i32; cap]; let mut list = vec![0i32; cap];
let list_c = list.as_mut_ptr() as *mut i32; let list_c: *mut i32 = list.as_mut_ptr();
std::mem::forget(list); std::mem::forget(list);
let mut occ_num: i32 = 0; let mut occ_num: i32 = 0;
@ -120,10 +120,10 @@ impl Bitfield {
let d1 = self.as_ptr(); let d1 = self.as_ptr();
let cap = self.data.len() / 2 * 64; let cap = self.data.len() / 2 * 64;
let mut b = vec![0i32; cap]; let mut b = vec![0i32; cap];
let list_up_c = b.as_mut_ptr() as *mut i32; let list_up_c: *mut i32 = b.as_mut_ptr();
std::mem::forget(b); std::mem::forget(b);
let mut b = vec![0i32; cap]; let mut b = vec![0i32; cap];
let list_dn_c = b.as_mut_ptr() as *mut i32; let list_dn_c: *mut i32 = b.as_mut_ptr();
std::mem::forget(b); std::mem::forget(b);
let mut occ_num_up: i32 = 0; let mut occ_num_up: i32 = 0;

View File

@ -192,12 +192,12 @@ impl File {
.try_into() .try_into()
.expect("try_into failed in write_determinant_list"); .expect("try_into failed in write_determinant_list");
let mut one_d_array: Vec<i64> = Vec::with_capacity(determinants.len() * n_int); let mut one_d_array: Vec<i64> = Vec::with_capacity(determinants.len() * n_int);
for det in determinants.iter() { for det in determinants {
for i in det.as_vec().iter() { for i in det.as_vec() {
one_d_array.push(i.clone()); one_d_array.push(*i);
} }
} }
let dset: *const i64 = one_d_array.as_ptr() as *const i64; let dset: *const i64 = one_d_array.as_ptr();
let rc = unsafe { c::trexio_write_determinant_list(self.ptr, offset, buffer_size, dset) }; let rc = unsafe { c::trexio_write_determinant_list(self.ptr, offset, buffer_size, dset) };
rc_return((), rc) rc_return((), rc)
} }
@ -251,7 +251,7 @@ impl File {
.chunks(2 * n_int) .chunks(2 * n_int)
.collect::<Vec<_>>() .collect::<Vec<_>>()
.iter() .iter()
.map(|x| (Bitfield::from_vec(&x))) .map(|x| (Bitfield::from_vec(x)))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
rc_return(result, rc) rc_return(result, rc)
} }