Skip to content

Conversation

@froydnj
Copy link
Contributor

@froydnj froydnj commented Dec 20, 2025

Using prism in rubyfmt has shown up a number of places where we have to do v.iter().next().unwrap() or similar. Using v.iter().last().unwrap() is also pretty sad, because we're asking the compiler to generate a loop for something that should be a simple memory load.

std::vec::Vec implements these little helper functions, so we might as well implement them on vector-like things here, too.

Note that the NodeList functions can't be const because Node::new can panic, which means that Node::new cannot be const.

cc @reese

Copy link
Collaborator

@kddnewton kddnewton 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!

A question for you: do you think we should share this functionality between NodeList and ConstantList with a trait? It seems like we're going to keep treating them as vector-like things, and we could have a more generic vector with an element size and it would all work in theory?

@kddnewton kddnewton merged commit 57afd7c into ruby:main Dec 22, 2025
64 checks passed
@froydnj froydnj deleted the froydnj-vec-first-last branch December 22, 2025 14:35
@froydnj
Copy link
Contributor Author

froydnj commented Dec 22, 2025

A question for you: do you think we should share this functionality between NodeList and ConstantList with a trait? It seems like we're going to keep treating them as vector-like things, and we could have a more generic vector with an element size and it would all work in theory?

I'm a bit divided here: having a trait for just two things feels like a little Too Much Rust. OTOH, it would have been nice to write at() and then get first() and last() for basically free.

The other thing that gives me pause is that I'm not sure how many more opportunities for code sharing there would be: AIUI, these structs can't implement a lot of Vec-like functionality (e.g. implementing Index) because we're handing out newly materialized objects, rather than direct references to the elements. If you look through the list of functions on Vec or slice, that doesn't leave you a whole lot of options.

I think it's something to keep an eye on.

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.

2 participants