From 100d347d2fa1fe22a59d17990b5508973e0ccf31 Mon Sep 17 00:00:00 2001 From: fengyang Date: Sat, 28 Jan 2023 17:08:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?LinkedList=20=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=94=B9=E4=B8=BA=20Status::wrapper=5Fstr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/linked_list.rs | 9 +++------ src/util/status.rs | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/util/linked_list.rs b/src/util/linked_list.rs index a97ac60..5a797fb 100644 --- a/src/util/linked_list.rs +++ b/src/util/linked_list.rs @@ -284,8 +284,7 @@ impl LinkedListBuilder for LinkedList { let len = self.length; if position > len { - return Err(Status::wrapper(LevelError::KInvalidArgument, - Slice::from(String::from("IndexOutOfRange")))); + return Err(Status::wrapper_str(LevelError::KInvalidArgument, "IndexOutOfRange")); } if position == 0 { @@ -338,8 +337,7 @@ impl LinkedListBuilder for LinkedList { let len = self.length; if position >= len { - return Err(Status::wrapper(LevelError::KInvalidArgument, - Slice::from(String::from("IndexOutOfRange")))); + return Err(Status::wrapper_str(LevelError::KInvalidArgument, "IndexOutOfRange")); } // Iterate towards the node at the given index, either from the start or the end, @@ -388,8 +386,7 @@ impl LinkedList { let len = self.length; if position >= len { - return Err(Status::wrapper(LevelError::KInvalidArgument, - Slice::from(String::from("IndexOutOfRange")))); + return Err(Status::wrapper_str(LevelError::KInvalidArgument, "IndexOutOfRange")); } // Iterate towards the node at the given index, either from the start or the end, diff --git a/src/util/status.rs b/src/util/status.rs index 4af5597..7965573 100644 --- a/src/util/status.rs +++ b/src/util/status.rs @@ -22,19 +22,39 @@ impl Default for Status { } impl Status { + /// 封装 LevelError 和 错误描述,得到 Status /// + /// # Arguments + /// + /// * `err`: LevelError 错误码 + /// * `str`: 错误描述 + /// + /// returns: Status + /// + /// # Examples + /// + /// ``` + /// Status::wrapper_str(LevelError::KInvalidArgument, "IndexOutOfRange"); + /// ``` + pub fn wrapper_str(err: LevelError, mut str: &str) -> Status { + Status::wrapper(err, str.into()) + } + + /// 封装 LevelError 和 错误描述,得到 Status /// /// # Arguments /// - /// * `err`: - /// * `slice`: + /// * `err`: LevelError 错误码 + /// * `slice`: 错误描述 /// /// returns: Status /// /// # Examples /// /// ``` + /// Status::wrapper(LevelError::KCorruption, "bad record, crc check failed".into()); /// + /// Status::wrapper(LevelError::KInvalidArgument, "IndexOutOfRange".into()); /// ``` pub fn wrapper(err: LevelError, mut slice: Slice) -> Status { if err.is_ok() { -- Gitee From 9c137e7c7823e95934b89ebe9aef830ba5386393 Mon Sep 17 00:00:00 2001 From: fengyang Date: Sat, 28 Jan 2023 17:09:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=86=85=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/linked_list.rs | 6 ++++++ src/util/status.rs | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/util/linked_list.rs b/src/util/linked_list.rs index 5a797fb..10480dc 100644 --- a/src/util/linked_list.rs +++ b/src/util/linked_list.rs @@ -216,6 +216,7 @@ impl Default for LinkedList { } impl LinkedListBuilder for LinkedList { + #[inline] fn new() -> Self { Self { length: 0, @@ -224,18 +225,22 @@ impl LinkedListBuilder for LinkedList { } } + #[inline] fn length(&self) -> usize { self.length } + #[inline] fn add(&mut self, val: T) { self.add_last(val).unwrap(); } + #[inline] fn push(&mut self, val: T) { self.add_first(val).unwrap(); } + #[inline] fn add_first(&mut self, val: T) -> Result { // 使用入参中的 val 创建一个链表节点Node,为了方便后续直接从 Box 获取到 raw ptr 裸指针, 使用 Box 包装 let mut node = Box::new(Node::new(val)); @@ -262,6 +267,7 @@ impl LinkedListBuilder for LinkedList { Ok(true) } + #[inline] fn add_last(&mut self, val: T) -> Result { let mut node = Box::new(Node::new(val)); diff --git a/src/util/status.rs b/src/util/status.rs index 7965573..2049335 100644 --- a/src/util/status.rs +++ b/src/util/status.rs @@ -36,6 +36,7 @@ impl Status { /// ``` /// Status::wrapper_str(LevelError::KInvalidArgument, "IndexOutOfRange"); /// ``` + #[inline] pub fn wrapper_str(err: LevelError, mut str: &str) -> Status { Status::wrapper(err, str.into()) } @@ -56,6 +57,7 @@ impl Status { /// /// Status::wrapper(LevelError::KInvalidArgument, "IndexOutOfRange".into()); /// ``` + #[inline] pub fn wrapper(err: LevelError, mut slice: Slice) -> Status { if err.is_ok() { slice = Slice::default(); -- Gitee