One step closer to generic mutability -- and looking for contributions

I finished implementing and published generic-mutability, a crate that allows API authors to create APIs that are generic over mutability.

I'm looking for feedback and help finishing this project.

This is just a cross-post, the main announcement is on URLO.

4 Likes

That's a lot of #[inline(always)] attributes. Unless you have benchmarks showing that #[inline(always)] improves performance, I would recommend just sticking with #[inline].

Also, you might be interested in using a union instead of a wrapper around a NonNull. I've put together a basic example if you want to test that out. It's not a finished product, but it should work as a proof-of-concept.

I don't have any benchmarks. I added them to make this whole thing "more zero-cost", but I guess leaving it up to the compiler might be better. Although IIRC even #[inline(always)] doesn't always inline. I think I'm going to change everything to #[inline], and we can add #[inline(always)] back where we need to (once we do have benchmarks).

I did actually think about using unions, but I can't recall exactly why I scrapped the idea. Being able to implement Copy and NonNull having a niche were possibly things influencing my decision. I dropped another idea [1] to get Copy, as I wanted GenRef to be a drop-in replacement for references. Syntax-wise it isn't, but on the type level it almost [2] is.

I'm not going to rush to change anything to match your example, but I'm keeping your ideas, in case they offer some objective benefit. Once I get more feedback, I'm also going to make some polls to let the community decide the shed color, and not tie it down to my personal preferences.


  1. ErasedMutRef, a struct made of a NonNull with a PhantomData lifetime and no mutability indicator; GenRef was going to have one of these inside it, but ErasedMutRef couldn't be soundly Copy ↩︎

  2. apart from requiring Sync for Send even for Mutable references, and having some default-implemented trait methods ↩︎

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.