Skip to content

Commit

Permalink
chore: correct RepeatedField origin (#3838)
Browse files Browse the repository at this point in the history
* chore: correct RepeatedField origin

Signed-off-by: tison <wander4096@gmail.com>

* fixup

Signed-off-by: tison <wander4096@gmail.com>

---------

Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Apr 30, 2024
1 parent a0f4881 commit d11b1fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/common/base/src/secrets.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
57 changes: 29 additions & 28 deletions src/servers/src/repeated_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,6 +75,12 @@ impl<T> RepeatedField<T> {
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) {
Expand Down Expand Up @@ -121,13 +128,13 @@ impl<T> RepeatedField<T> {

/// 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]
}

Expand Down Expand Up @@ -169,13 +176,13 @@ impl<T> RepeatedField<T> {

/// 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)
}

Expand All @@ -193,13 +200,13 @@ impl<T> RepeatedField<T> {

/// 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]
}
Expand Down Expand Up @@ -252,7 +259,7 @@ impl<T> RepeatedField<T> {
/// # 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);
Expand Down Expand Up @@ -282,22 +289,15 @@ impl<T> RepeatedField<T> {
self.as_mut_slice().reverse()
}

/// Into owned iterator.
#[inline]
pub fn into_iter(mut self) -> vec::IntoIter<T> {
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<T> {
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<T> {
self.as_mut_slice().iter_mut()
}

Expand Down Expand Up @@ -327,7 +327,7 @@ impl<T: Default + Clear> RepeatedField<T> {
/// 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 {
Expand All @@ -352,10 +352,10 @@ impl<'a, T: Clone> From<&'a [T]> for RepeatedField<T> {
}
}

impl<T> Into<Vec<T>> for RepeatedField<T> {
impl<T> From<RepeatedField<T>> for Vec<T> {
#[inline]
fn into(self) -> Vec<T> {
self.into_vec()
fn from(val: RepeatedField<T>) -> Self {
val.into_vec()
}
}

Expand Down Expand Up @@ -414,12 +414,13 @@ impl<'a, T> IntoIterator for &'a mut RepeatedField<T> {
}
}

impl<'a, T> IntoIterator for RepeatedField<T> {
impl<T> IntoIterator for RepeatedField<T> {
type Item = T;
type IntoIter = vec::IntoIter<T>;

fn into_iter(self) -> vec::IntoIter<T> {
self.into_iter()
fn into_iter(mut self) -> vec::IntoIter<T> {
self.vec.truncate(self.len);
self.vec.into_iter()
}
}

Expand Down Expand Up @@ -460,7 +461,7 @@ impl<T: Hash> Hash for RepeatedField<T> {

impl<T> AsRef<[T]> for RepeatedField<T> {
#[inline]
fn as_ref<'a>(&'a self) -> &'a [T] {
fn as_ref(&self) -> &[T] {
&self.vec[..self.len]
}
}
Expand Down Expand Up @@ -491,14 +492,14 @@ impl<T> Index<usize> for RepeatedField<T> {
type Output = T;

#[inline]
fn index<'a>(&'a self, index: usize) -> &'a T {
fn index(&self, index: usize) -> &T {
&self.as_ref()[index]
}
}

impl<T> IndexMut<usize> for RepeatedField<T> {
#[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]
}
}
Expand Down

0 comments on commit d11b1fa

Please sign in to comment.