Contributing to Artichoke in Rust

Yesterday, I eked out a bit of time to contribute to Artichoke in Rust, in no small part due to the maintainer @lopopolo:

Brian Kung (he/him/his): Hi there! I'm a rubyist learning Rust

lopopolo: Welcome to the party

Brian Kung: I'd like to contribute if possible someday 😊

lopopolo: Yay

lopopolo: If you're thinking someday might be today (🤩), how does this ticket look? https://github.com/artichoke/artichoke/issues/1500
@lopopolo was very welcoming!

Artichoke is an implementation of Ruby written in the Rust programming language. The issue I worked on was to expose a newly stabilized method in Artichoke’s implementation of the Ruby String class. In Rust, strings are represented as Vectors of u8 bytes which are guaranteed to be valid UTF-8 sequences. As of Rust version 1.56.0, the compiler stabilized the Vec::shrink_to method, which allows you to reduce the amount of memory a Vec takes, down to a lower bound, which you supply. So if you have a String and it has allocated 4 bytes, but it’s only taking up 1 byte, you can string.shrink_to(2) to reduce its size to no less than 2 bytes. Here’s that same scenario in code:

let mut string = String::with_capacity(4);
string.extend("1".chars());
assert_eq!(string.capacity(), 4);

string.shrink_to(2);
assert!(string.capacity() >= 2);

string.shrink_to(0);
assert!(string.capacity() >= 1);

(Rust Playground Link)

The code I worked on exposed the same methods on Artichoke’s String classes: Implements `shrink_to` on `spinoso_string::String` by briankung · Pull Request #1505 · artichoke/artichoke. The actual code, excluding the doctests, was very minimal:

#[inline]
pub fn shrink_to(&mut self, min_capacity: usize) {
    self.buf.shrink_to(min_capacity);
}
lopopolo: woohoo! this looks great

Brian Kung (he/him/his): Thanks for the push! Feels good to contribute 💪😂

lopopolo: 🎉

lopopolo: I left one ask for some changes. Since we've added a new API, let's bump the version of `spinoso-string`.

Brian Kung: sounds good
After a few tweaks, my PR was good to go!

But what I liked about this was how easy @lopopolo made it to contribute! From giving me the initial nudge and motivation to build Artichoke locally for the first time to the extensive guidance in the issue, it was a great way to get some open source experience on a non-trivial Rust project. I can’t recommend it enough. In fact, I can point you to a few issues that could use a similar treatment:

Image of similar issues on artichoke ruby
These would require basically the same code changes as the above, and they are still open as of Saturday, November 20th, 2021!

This was a great experience for me, and @lopopolo’s stewardship of the project reminded me of both Ruby’s value of niceness and Rust’s value of inclusivity. It led directly to a second PR and I hope to keep contributing in the future to learn more about both Rust and Ruby.

Links:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: