3 ms·
We can talk in specifics: this example was about making people use unsafe to avoid having to type <'static> in the general case. The author had been trying for
by cormacrelf 4y ago
We can talk in specifics: this example was about making people use unsafe to avoid having to type <'static> in the general case. The author had been trying for a number of paragraphs to avoid polluting the type name with generics or lifetimes. This is what going too far looks like.
The std APIs look like this:
struct Box<T, A: Allocator = Global>
And it’s been this way in stable releases for the last year or so. The same has been done for Vec and all the other std::collections. What percent of Rust programmers do you think even noticed at all? 1%? The most flexible design and the least impacting on regular users. You can use A = &'a dyn Allocator if you like, equally you can choose a ZST and not pay for 16 bytes of storage. The library author has no need to choose in advance at all, which is great if they’re determined to make weirdly constrained choices, ultimately forcing most uses of the API to be unsafe. I stopped reading after that so I don’t know which design they went for.
- mikepurvis 4y agoAnd if the generated documentation is really the main sticking point, it would be perfectly possible to special case the allocator type parameter in rustdoc so that it hides or otherwise deemphasizes it.
- throwawaymaths 4y ago> The most flexible design Not quite. For the most general case, you probably want allocator to be a proper parameter instead of a type parameter. For example, suppose you have a green thread that you want to have its own isolated heap. Then you can't assume that any given allocator is a singleton in its type; The struct itself must somehow be able to find to its "owner" on release. In the green thread case you can't "just use threadlocal" because a green thread might not be sticky to an os thread.
- twic 4y agoRust already has almost exactly this problem with keeping track of the current task in the async framework. As far as I know, the solution is indeed to use a thread local, and be scrupulous about updating it on task entry. It's ugly but it seems to work.
- cormacrelf 4y agoThey thought of that. There are Box::new_in variants for all the other ways of creating a Box. You can put data in there to identify a green thread’s heap. https://doc.rust-lang.org/stable/std/boxed/struct.Box.html#method.new_in https://doc.rust-lang.org/stable/std/boxed/struct.Box.html#m...