diff --git a/src/util/linked_list.rs b/src/util/linked_list.rs index a97ac60e505f4b7e64daa58ec292a3c1079f92d5..10480dc2a3f798555576e289d3be21adec536d27 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)); @@ -284,8 +290,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 +343,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 +392,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 4af55973969df732fb9aa9e9a58cd463d09bd2e1..20493355fe69b7d68d1283013845b385c613d43f 100644 --- a/src/util/status.rs +++ b/src/util/status.rs @@ -22,20 +22,42 @@ impl Default for Status { } impl Status { + /// 封装 LevelError 和 错误描述,得到 Status /// + /// # Arguments + /// + /// * `err`: LevelError 错误码 + /// * `str`: 错误描述 + /// + /// returns: Status + /// + /// # Examples + /// + /// ``` + /// Status::wrapper_str(LevelError::KInvalidArgument, "IndexOutOfRange"); + /// ``` + #[inline] + 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()); /// ``` + #[inline] pub fn wrapper(err: LevelError, mut slice: Slice) -> Status { if err.is_ok() { slice = Slice::default();