Somewhat unrelated to the OP, but there’s indeed a pattern that could be served neither by newtype wrappers, nor by the proposed fn parameter API.
Consider, for example, this:
struct Entity {
name: String,
size: usize,
// ...
}
struct Scene {
entities: Vec<Entity>,
entities_by_name: BTreeSet<u32>,
entities_by_size: BTreeSet<u32>,
}
That is, I want to store a number of entities, and I want to order them by two properties. To avoid duplication, ideally I’d love to store entities in a single array and use indices. However, I just can’t write a newtype wrapper for u32 to do this, because the wrapper will need access to entities, and you have nothing to work with except for u32. Note as well that I can’t use &Entity instead of u32, b/c that’ll run into “storing owner and ref in a single object” issue.
I think the most general API, which would allow for such usages, is to accept closure with ops on the call site:
map.insert_with_cmp(92, |&i, &j| self.entities[i].name.cmp(&self.entities[j].name));
I’d love to see a crate.io crate with such APIs! The particular use case I had was to write a string interner like
struct Interner {
data: Vec<String>,
map: HashSet<u32>,
}