Skip to content

ListView: add deref#113

Merged
grod220 merged 2 commits intomainfrom
sorting-methods
Aug 6, 2025
Merged

ListView: add deref#113
grod220 merged 2 commits intomainfrom
sorting-methods

Conversation

@grod220
Copy link
Member

@grod220 grod220 commented Aug 5, 2025

While working on auction mechanics, found myself needing a few new methods in ListView:

  • get(&self, index: usize)
  • get_mut(&mut self, index: usize)
  • as_mut_slice(&mut self)
  • sort(&mut self) and sort_by(&mut self, ...)

This PR adds these methods to the respective trait + mut views.

=======

EDIT: Adding deref trait implementation to ListViewMut & ListViewReadOnly to get the benefits above implicitly.

joncinque
joncinque previously approved these changes Aug 5, 2025
Copy link
Contributor

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks good to me! Two things that you can take or leave

Comment on lines +59 to +62
/// Returns a mutable reference to an element at a given index.
/// Returns `None` if the index is out of bounds of the current length.
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
self.as_mut_slice().get_mut(index)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Up to you, but I don't find these kinds of functions that just wrap over slice particularly useful, since someone can just use as_mut_slice() and then whatever function they want afterward.

iter_mut() and sort() and sort_by() could equally be removed

Copy link
Contributor

@buffalojoec buffalojoec Aug 6, 2025

Choose a reason for hiding this comment

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

Personally, I like these. I would keep them. It's a much better devex and feels more native to Vec.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good point, maintaining all these proxy methods does not scale very well. To keep the nice shorthand though, how about a deref impl? Have a look 👀

buffalojoec
buffalojoec previously approved these changes Aug 6, 2025
Comment on lines +59 to +62
/// Returns a mutable reference to an element at a given index.
/// Returns `None` if the index is out of bounds of the current length.
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
self.as_mut_slice().get_mut(index)
}
Copy link
Contributor

@buffalojoec buffalojoec Aug 6, 2025

Choose a reason for hiding this comment

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

Personally, I like these. I would keep them. It's a much better devex and feels more native to Vec.

@grod220 grod220 dismissed stale reviews from buffalojoec and joncinque via a8d5892 August 6, 2025 08:38
@grod220 grod220 changed the title ListView: get & sorting methods ListView: add deref Aug 6, 2025
Copy link
Contributor

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks good to me too!

Deref can be tough for downstream developers to reason about, so I would include it with the caveat that there's a chance of removing it later.

assert_eq!(view_ro.capacity(), capacity);
assert!(view_ro.is_empty());
assert_eq!(view_ro.as_slice(), &[] as &[u32]);
assert_eq!(&*view_ro, &[] as &[u32]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Seeing this kind of syntax gives me flashbacks to doing weird things with Arcs while learning Rust, but it's not the worst thing in the world, since someone can also call .deref() by hand

@grod220
Copy link
Member Author

grod220 commented Aug 6, 2025

I would include it with the caveat that there's a chance of removing it later.

Sorry for clarity @joncinque, are you suggesting we add code comments to the Deref impls saying something like:

/// **Note**: This API is a convenience and may be removed in a future version
/// if it is found to be a source of confusion.

or is that caveat just for us to keep in mind?

@joncinque
Copy link
Contributor

Oh sorry, mainly for us to keep in mind

Copy link
Contributor

@buffalojoec buffalojoec left a comment

Choose a reason for hiding this comment

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

Honestly I thought .as_slice() was more intuitive. You could use AsSlice and AsMutSlice.

I also didn't think having a few very common higher-level helpers for convenient devex was a bad idea either.

But I won't hold up the PR, feel free to merge as-is if you both like it better.

@grod220
Copy link
Member Author

grod220 commented Aug 6, 2025

Let's see how it goes and can revise if we find another API serves actual usecases better. I don't fear the major version bump 😅

@grod220 grod220 merged commit 19ffb77 into main Aug 6, 2025
29 checks passed
@grod220 grod220 deleted the sorting-methods branch August 6, 2025 12:28
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.

3 participants