From 2867d40768df367e6b25865bcc442187e180e0ff Mon Sep 17 00:00:00 2001 From: paq <89paku@gmail.com> Date: Sat, 13 Feb 2021 17:56:12 +0900 Subject: [PATCH 1/6] avoid unwrap --- src/booster.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/booster.rs b/src/booster.rs index ff86b42..dbebfc9 100644 --- a/src/booster.rs +++ b/src/booster.rs @@ -159,7 +159,7 @@ impl Booster { /// Get Feature Names. pub fn feature_name(&self) -> Result> { - let num_feature = self.num_feature().unwrap(); + let num_feature = self.num_feature()?; let feature_name_length = 32; let mut num_feature_names = 0; let mut out_buffer_len = 0; @@ -187,7 +187,7 @@ impl Booster { // Get Feature Importance pub fn feature_importance(&self) -> Result> { - let num_feature = self.num_feature().unwrap(); + let num_feature = self.num_feature()?; let out_result: Vec = vec![Default::default(); num_feature as usize]; lgbm_call!(lightgbm_sys::LGBM_BoosterFeatureImportance( self.handle, From 9ac9f185da57340994cdff9c368e1830f3248b71 Mon Sep 17 00:00:00 2001 From: paq <89paku@gmail.com> Date: Sat, 13 Feb 2021 18:00:15 +0900 Subject: [PATCH 2/6] remove unnecessary let binding --- src/booster.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/booster.rs b/src/booster.rs index dbebfc9..ba9d1a5 100644 --- a/src/booster.rs +++ b/src/booster.rs @@ -231,8 +231,7 @@ mod tests { fn _train_booster(params: &Value) -> Booster { let dataset = _read_train_file().unwrap(); - let bst = Booster::train(dataset, ¶ms).unwrap(); - bst + Booster::train(dataset, ¶ms).unwrap() } fn _default_params() -> Value { From 5b2a91eb0beae987917aadb8f4579abb44876289 Mon Sep 17 00:00:00 2001 From: paq <89paku@gmail.com> Date: Sat, 13 Feb 2021 18:01:34 +0900 Subject: [PATCH 3/6] remove unneeded format! --- src/error.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 1b20a78..96e66ce 100644 --- a/src/error.rs +++ b/src/error.rs @@ -29,10 +29,7 @@ impl Error { match ret_val { 0 => Ok(()), -1 => Err(Error::from_lightgbm()), - _ => panic!(format!( - "unexpected return value '{}', expected 0 or -1", - ret_val - )), + _ => panic!("unexpected return value '{}', expected 0 or -1", ret_val), } } From f8e940840388ed4ee1bab6d2aad8036808fba1aa Mon Sep 17 00:00:00 2001 From: paq <89paku@gmail.com> Date: Sat, 13 Feb 2021 18:15:22 +0900 Subject: [PATCH 4/6] suppress clippy warnings --- lightgbm-sys/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/lightgbm-sys/src/lib.rs b/lightgbm-sys/src/lib.rs index e1206e1..8d4e928 100644 --- a/lightgbm-sys/src/lib.rs +++ b/lightgbm-sys/src/lib.rs @@ -3,5 +3,6 @@ #![allow(non_snake_case)] #![allow(clippy::redundant_static_lifetimes)] #![allow(clippy::missing_safety_doc)] +#![allow(clippy::upper_case_acronyms)] include!(concat!(env!("OUT_DIR"), "/bindings.rs")); From 80a82ad7a37751e67cf412bb7a230d07544de6be Mon Sep 17 00:00:00 2001 From: paq <89paku@gmail.com> Date: Sat, 13 Feb 2021 18:17:41 +0900 Subject: [PATCH 5/6] change visibility --- src/booster.rs | 4 ++-- src/dataset.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/booster.rs b/src/booster.rs index ba9d1a5..bd1732b 100644 --- a/src/booster.rs +++ b/src/booster.rs @@ -6,11 +6,11 @@ use serde_json::Value; use lightgbm_sys; -use super::{Dataset, Error, Result}; +use crate::{Dataset, Error, Result}; /// Core model in LightGBM, containing functions for training, evaluating and predicting. pub struct Booster { - pub(super) handle: lightgbm_sys::BoosterHandle, + handle: lightgbm_sys::BoosterHandle, } impl Booster { diff --git a/src/dataset.rs b/src/dataset.rs index ae1a06b..f89fcbf 100644 --- a/src/dataset.rs +++ b/src/dataset.rs @@ -3,7 +3,7 @@ use lightgbm_sys; use std; use std::ffi::CString; -use super::{Error, Result}; +use crate::{Error, Result}; /// Dataset used throughout LightGBM for training. /// @@ -31,7 +31,7 @@ use super::{Error, Result}; /// let dataset = Dataset::from_file(&"lightgbm-sys/lightgbm/examples/binary_classification/binary.train").unwrap(); /// ``` pub struct Dataset { - pub(super) handle: lightgbm_sys::DatasetHandle, + pub(crate) handle: lightgbm_sys::DatasetHandle, } #[link(name = "c")] From d4fbadc4ccc1c57692351447d0f96a0374d96958 Mon Sep 17 00:00:00 2001 From: paq <89paku@gmail.com> Date: Sat, 13 Feb 2021 18:18:29 +0900 Subject: [PATCH 6/6] use Self --- src/dataset.rs | 6 +++--- src/error.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/dataset.rs b/src/dataset.rs index f89fcbf..aa56974 100644 --- a/src/dataset.rs +++ b/src/dataset.rs @@ -37,7 +37,7 @@ pub struct Dataset { #[link(name = "c")] impl Dataset { fn new(handle: lightgbm_sys::DatasetHandle) -> Self { - Dataset { handle } + Self { handle } } /// Create a new `Dataset` from dense array in row-major order. @@ -82,7 +82,7 @@ impl Dataset { lightgbm_sys::C_API_DTYPE_FLOAT32 as i32 ))?; - Ok(Dataset::new(handle)) + Ok(Self::new(handle)) } /// Create a new `Dataset` from file. @@ -116,7 +116,7 @@ impl Dataset { &mut handle ))?; - Ok(Dataset::new(handle)) + Ok(Self::new(handle)) } } diff --git a/src/error.rs b/src/error.rs index 96e66ce..1c6bee3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,7 +17,7 @@ pub struct Error { impl Error { pub(crate) fn new>(desc: S) -> Self { - Error { desc: desc.into() } + Self { desc: desc.into() } } /// Check the return value from an LightGBM FFI call, and return the last error message on error. @@ -28,7 +28,7 @@ impl Error { pub(crate) fn check_return_value(ret_val: i32) -> Result<()> { match ret_val { 0 => Ok(()), - -1 => Err(Error::from_lightgbm()), + -1 => Err(Self::from_lightgbm()), _ => panic!("unexpected return value '{}', expected 0 or -1", ret_val), } }