Rust: Provide a trait method to (optionally) control resource allocation#1625
Conversation
alexcrichton
left a comment
There was a problem hiding this comment.
I like the idea, thanks! I've got some comments below, but let me know if you'd prefer to not handle anything in particular.
|
@cpetig ok I tried my hand a this a bit and resulted in the last commit here. Everything looks good to me now but I'd want to run that by you to confirm. I realized that ideally what I want is: trait GuestThing {
type Rep: ResourceRep = Box<Option<T>>;
// ...
}however defaults for associated bounds aren't stable in Rust. In lieu of that I did a, now unrelated, refactoring to the I also realized that the One possible future option is to basically require all Rust exported resources to manually say |
It amuses me that you took my frustration with the incompleteness of traits in stable Rust to the next level, yesterday I was just sad that defaults for associated types were not possible in this PR, but a bound on an associated type is a concept I didn't miss until today.
Oh, another facet of unsafe which I didn't see before. An unsafe trait would make sense, because you rely on the correctness of the implementation of two particular functions, but you don't want to make the trait implementation unsafe for all people not overriding the allocation method.
That feels excessive, but also missing specialization has made one of my API designs less elegant than I wished for before. 🤷 Thank you for teaching me new aspects of Rust, I will need some hours to read and understand your new design. |
|
Looks elegant to me. I think documentation always improves when it written by more than a single individual, you pointed out so many things I have forgotten to mention. |
| Self::type_guard::<T>(); | ||
| let _ = unsafe {{ T::resource_from_raw_(handle as *mut _{camel}Rep<T>) }}; | ||
| unsafe {{ | ||
| let _rep = T::resource_from_raw_(handle.cast()); |
There was a problem hiding this comment.
This is the one change which makes me curious, I tried to minimize the number of characters in the unsafe block. Why would you put the let into the unsafe block instead?
There was a problem hiding this comment.
Ah mostly just subjective on this. It's all generated code anyway and doesn't really get // SAFETY: ... comments regardless so I basically just took the expedient route here. Otherwise though I'd agree that this would probably best be served by 2 blocks.
alexcrichton
left a comment
There was a problem hiding this comment.
Ok I'll go ahead and flag this for merge, thanks for putting up with me here @cpetig and thanks again!
This enables resource allocation from a user provided arena instead of the heap.
Arena implementation in test case generated by genAI.