Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Buffer::slice_ref #2072

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open

Add Buffer::slice_ref #2072

wants to merge 5 commits into from

Conversation

robert3005
Copy link
Member

@robert3005 robert3005 commented Jan 24, 2025

And fix PartialEq, PartialOrd, Hash for Buffer

fix #2009

@robert3005 robert3005 requested a review from gatesn January 24, 2025 18:11
Comment on lines 23 to 42
impl<T> PartialEq for Buffer<T> {
fn eq(&self, other: &Self) -> bool {
self.bytes == other.bytes
}
}

impl<T> Eq for Buffer<T> {}

impl<T> PartialOrd for Buffer<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.bytes.partial_cmp(&other.bytes)
}
}

impl<T> Hash for Buffer<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.bytes.as_ref().hash(state)
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think previously we were wrong where we would compare alignment and values

pub struct Buffer<T> {
pub(crate) bytes: Bytes,
pub(crate) length: usize,
pub(crate) alignment: Alignment,
pub(crate) _marker: std::marker::PhantomData<T>,
}

impl<T> PartialEq for Buffer<T> {
fn eq(&self, other: &Self) -> bool {
self.bytes == other.bytes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know what the Rust convention is here. Should the default compare everything (including alignment) and an extra function eq_bytes ignores it? Or the other way around?

@@ -10,14 +12,32 @@ use crate::debug::TruncatedDebug;
use crate::{Alignment, BufferMut, ByteBuffer};

/// An immutable buffer of items of `T`.
#[derive(Clone, PartialEq, Eq, PartialOrd, Hash)]
#[derive(Clone, Eq)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May as well Ord too

Copy link
Contributor

@gatesn gatesn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a nasty gotcha I think. Is there some private constructor we need that just validates alignment? Instead of allowing Self { ... } construction?

/// # Panics:
/// Requires that the given sub slice is in fact contained within the Bytes buffer; otherwise this function will panic.
pub fn slice_ref(&self, subset: &[T]) -> Self {
let subset_u8 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're forgetting to check the new alignment.

See slice - you cannot end up with a Buffer that doesn't respect the defined alignment so you must also panic if the subset_u8 isn't aligned per the current alignment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is a bug, though we want to support slicing with alignment between align_of:: and self.alignment. In particular for varbinviews the buffer will have alignment of 16 but the slice will have alignment of at most 4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add slice_ref to Buffer
3 participants