From d11b1fa389c54381c46fa17894b05fc21536815b Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 30 Apr 2024 11:13:54 +0800 Subject: [PATCH] chore: correct RepeatedField origin (#3838) * chore: correct RepeatedField origin Signed-off-by: tison * fixup Signed-off-by: tison --------- Signed-off-by: tison --- src/common/base/src/secrets.rs | 2 +- src/servers/src/lib.rs | 3 +- src/servers/src/repeated_field.rs | 57 ++++++++++++++++--------------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/common/base/src/secrets.rs b/src/common/base/src/secrets.rs index 49c9b37de35b..702b6d3ccd8a 100644 --- a/src/common/base/src/secrets.rs +++ b/src/common/base/src/secrets.rs @@ -1,4 +1,4 @@ -// This file is copied from: https://github.com/iqlusioninc/crates/blob/f98d4ccf/secrecy/src/lib.rs. +// This file is copied from https://github.com/iqlusioninc/crates/blob/f98d4ccf/secrecy/src/lib.rs //! [`SecretBox`] wrapper type for more carefully handling secret values //! (e.g. passwords, cryptographic keys, access tokens or other credentials) diff --git a/src/servers/src/lib.rs b/src/servers/src/lib.rs index 0459ca2a3f6c..f738c1f4373a 100644 --- a/src/servers/src/lib.rs +++ b/src/servers/src/lib.rs @@ -40,8 +40,7 @@ pub mod prom_store; pub mod prometheus_handler; pub mod proto; pub mod query_handler; -#[allow(clippy::all)] -mod repeated_field; +pub mod repeated_field; mod row_writer; pub mod server; mod shutdown; diff --git a/src/servers/src/repeated_field.rs b/src/servers/src/repeated_field.rs index 8346427a3368..1b8f80bea274 100644 --- a/src/servers/src/repeated_field.rs +++ b/src/servers/src/repeated_field.rs @@ -18,8 +18,9 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE // OR OTHER DEALINGS IN THE SOFTWARE. -// The [Clear] trait and [RepeatedField] are taken from [rust-protobuf](https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-examples/vs-prost) -// to leverage the pooling mechanism to avoid frequent heap allocation/deallocation when decoding deeply nested structs. +// The Clear trait is copied from https://github.com/stepancheg/rust-protobuf/blob/v2.28.0/protobuf/src/clear.rs +// The RepeatedField struct is copied from https://github.com/stepancheg/rust-protobuf/blob/v2.28.0/protobuf/src/repeated.rs +// This code is to leverage the pooling mechanism to avoid frequent heap allocation/de-allocation when decoding deeply nested structs. use std::borrow::Borrow; use std::cmp::Ordering; @@ -74,6 +75,12 @@ impl RepeatedField { self.len } + /// Returns true if this container is empty. + #[inline] + pub fn is_empty(&self) -> bool { + self.len == 0 + } + /// Clear. #[inline] pub fn clear(&mut self) { @@ -121,13 +128,13 @@ impl RepeatedField { /// View data as slice. #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [T] { + pub fn as_slice(&self) -> &[T] { &self.vec[..self.len] } /// View data as mutable slice. #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { + pub fn as_mut_slice(&mut self) -> &mut [T] { &mut self.vec[..self.len] } @@ -169,13 +176,13 @@ impl RepeatedField { /// View this container as two slices split at given index. #[inline] - pub fn split_at<'a>(&'a self, mid: usize) -> (&'a [T], &'a [T]) { + pub fn split_at(&self, mid: usize) -> (&[T], &[T]) { self.as_ref().split_at(mid) } /// View this container as two mutable slices split at given index. #[inline] - pub fn split_at_mut<'a>(&'a mut self, mid: usize) -> (&'a mut [T], &'a mut [T]) { + pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) { self.as_mut_slice().split_at_mut(mid) } @@ -193,13 +200,13 @@ impl RepeatedField { /// Mutable last element of this container. #[inline] - pub fn last_mut<'a>(&'a mut self) -> Option<&'a mut T> { + pub fn last_mut(&mut self) -> Option<&mut T> { self.as_mut_slice().last_mut() } /// View all but last elements of this container. #[inline] - pub fn init<'a>(&'a self) -> &'a [T] { + pub fn init(&self) -> &[T] { let s = self.as_ref(); &s[0..s.len() - 1] } @@ -252,7 +259,7 @@ impl RepeatedField { /// # Examples /// /// ``` - /// # use protobuf::RepeatedField; + /// use servers::repeated_field::RepeatedField; /// /// let mut vec = RepeatedField::from(vec![1, 2, 3, 4]); /// vec.retain(|&x| x % 2 == 0); @@ -282,22 +289,15 @@ impl RepeatedField { self.as_mut_slice().reverse() } - /// Into owned iterator. - #[inline] - pub fn into_iter(mut self) -> vec::IntoIter { - self.vec.truncate(self.len); - self.vec.into_iter() - } - /// Immutable data iterator. #[inline] - pub fn iter<'a>(&'a self) -> slice::Iter<'a, T> { + pub fn iter(&self) -> slice::Iter { self.as_ref().iter() } /// Mutable data iterator. #[inline] - pub fn iter_mut<'a>(&'a mut self) -> slice::IterMut<'a, T> { + pub fn iter_mut(&mut self) -> slice::IterMut { self.as_mut_slice().iter_mut() } @@ -327,7 +327,7 @@ impl RepeatedField { /// Push default value. /// This operation could be faster than `rf.push(Default::default())`, /// because it may reuse previously allocated and cleared element. - pub fn push_default<'a>(&'a mut self) -> &'a mut T { + pub fn push_default(&mut self) -> &mut T { if self.len == self.vec.len() { self.vec.push(Default::default()); } else { @@ -352,10 +352,10 @@ impl<'a, T: Clone> From<&'a [T]> for RepeatedField { } } -impl Into> for RepeatedField { +impl From> for Vec { #[inline] - fn into(self) -> Vec { - self.into_vec() + fn from(val: RepeatedField) -> Self { + val.into_vec() } } @@ -414,12 +414,13 @@ impl<'a, T> IntoIterator for &'a mut RepeatedField { } } -impl<'a, T> IntoIterator for RepeatedField { +impl IntoIterator for RepeatedField { type Item = T; type IntoIter = vec::IntoIter; - fn into_iter(self) -> vec::IntoIter { - self.into_iter() + fn into_iter(mut self) -> vec::IntoIter { + self.vec.truncate(self.len); + self.vec.into_iter() } } @@ -460,7 +461,7 @@ impl Hash for RepeatedField { impl AsRef<[T]> for RepeatedField { #[inline] - fn as_ref<'a>(&'a self) -> &'a [T] { + fn as_ref(&self) -> &[T] { &self.vec[..self.len] } } @@ -491,14 +492,14 @@ impl Index for RepeatedField { type Output = T; #[inline] - fn index<'a>(&'a self, index: usize) -> &'a T { + fn index(&self, index: usize) -> &T { &self.as_ref()[index] } } impl IndexMut for RepeatedField { #[inline] - fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut T { + fn index_mut(&mut self, index: usize) -> &mut T { &mut self.as_mut_slice()[index] } }