core: generalize BorrowedCursor::ensure_init - #160432
Conversation
|
r? @clarfonthey rustbot has assigned @clarfonthey. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
|
||
| impl<T: Default + Copy> InitSpec for T { | ||
| default fn initialize(buf: &mut [MaybeUninit<Self>]) { | ||
| buf.write_with(|_| Self::default()); |
There was a problem hiding this comment.
Just looking at write_with, perhaps it would be nice to just have a general write_default that we could override for all slices, rather than just this type, so it could be reused elsewhere.
If you'd be okay doing that instead, adding an unstable/private method to slices instead?
There was a problem hiding this comment.
That seems very useful. I went ahead and added it publicly under the existing maybe_uninit_fill tracking issue since it's probably so uncontroversial.
This comment has been minimized.
This comment has been minimized.
5135717 to
0100285
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
CC @rust-lang/libs-api for the |
|
sure! we're happy with this addition. thanks ^^ |
|
Seems reasonable to me! We'd left |
| where | ||
| T: Default, | ||
| { | ||
| trait DefaultSpec: Default { |
There was a problem hiding this comment.
This feels close to something that exists for Vec but maybe I'm misremembering. Either way it might be worth not nesting in the function in case it becomes useful elsewhere. Kinda want to avoid the duplication if we can avoid it
There was a problem hiding this comment.
You are probably thinking of IsZero, which Vec::from_elem uses to check if it can use zeroed allocation. In this case however we don't have an existing value whose zeroness we could check but rather want to test a property of the Default implementation.
I guess I could add a marker trait if you'd like. But I'm sort of tempted to leave all that complexity for when someone complains...
There was a problem hiding this comment.
Nah, you're right, this is pretty unique. I'm down for that plan; I was just trying to remember if we had code somewhere that was similar enough.
Tracking issue: #160476
This implements the future possibility left out in #149749 (comment) and makes
ensure_initgeneric overDefault. I used specialisation to make sure the performance in theu8case stays equivalent tomemsetirrespective of how clever the optimiser happens to be.CC @joshtriplett as you've been working on this stuff recently.
This also adds a public
write_defaultmethod on[MaybeUninit<T>]that's doing the equivalent of.write_init(|_| Default::default()), but uses specialisation for integers. This uses the existing tracking issue formaybe_uninit_fill.