Feature: Add std.typecons.ManagedLifetime.#8674
Feature: Add std.typecons.ManagedLifetime.#8674FeepingCreature wants to merge 19 commits intodlang:masterfrom
Conversation
Otherwise alias this would not work outside std.typecons.
Wording was backward: `const S = S` is fine, `S = const S` disallowed.
Example of previous failing code:
struct S
{
immutable int i;
}
Rebindable!(const S) r = const S(1);
immutable int* p = &r.i;
r = S(5);
assert(*p == 1); //fails
|
Thanks for your pull request and interest in making D better, @FeepingCreature! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#8674" |
|
I removed the unittest that arrays don't work. Any type should work. Then we don't need weird things like The referenced bug just complained that the documentation didn't match the code. So I changed the documentation. |
|
Ripped out a lot of tests I didn't understand from the original PR. (If the contained type is const, why are you supposed to be able to call a nonconst destructor...?) |
Not 100% sure what's going on with the different ways of calling them.
|
Hm. I've tested it on our internal code, and it's probably not enough for our use. Rebindable doesn't have a notion of "a state where you know that the type is not initialized." So it can't be used as the backing store for |
|
I think to do it properly, we just need a new type in With a dedicated type that can have an externally defined empty state, we could then easily implement Idea is this: |
`ManagedLifetime` allows manual, explicit control over when the copy constructor and destructor of a contained type are run, regardless of whether the passed type is mutable or immutable.
|
ping @dkorpel @atilaneves also |
|
Before I look at the code: why in Phobos and not a dub package? |
|
Because the bugs we run into with lack of immutable support in algorithms are largely in Phobos, particularly It is also a dub package: https://code.dlang.org/packages/rebindable But I can't make The A few days ago, we ran into |
|
Hey, uh, ping? I understand that this may be controversial. If you're against it, feel free to say so and why! I don't like this solution either, I just don't see hope for another approach. |
|
@atilaneves what is your stance on this PR? |
|
Sorry, the diff is large and because of that I kept putting off. Then I had to carve out time to rewatch the DConf presentation to understand why this is desireable, and I agree that it is and needs to be in Phobos to fix algorithms such as But after all of that I confess my brain isn't capable of digesting the diff as-is and I'm wondering whether this can and should be broken down into smaller PRs. I'm not sure about the |
|
I mean, I'm not sure if it makes sense to make it not public. If it's not public, it'll still need to support every type, so if there's a bug it'll need to be fixed here as well as in the public version ( Does it sound good to break The problem with breaking out smaller parts than that is that it becomes increasingly difficult to understand the entire picture. edit: For the record also, I'm increasingly unconvinced that edit: The main upside of |
|
Ping @atilaneves Should I go ahead with a separate PR for |
|
Yes, I think so. This is definitely too large as it is unless I dedicate a morning to it. |
Add
std.typecons.ManagedLifetime(T).Use it to implement
Rebindablefor structs.Previous description
This is a rewrite, heavily based on https://github.com/FeepingCreature/rebindable/ , of #6136 which is itself a revival of #4363.
For rationale, see my 2022 DConf talk https://www.youtube.com/watch?v=eGX_fxlig8I
I'm not sure how much hope this has, but I figured I'd put it out there.
The major new piece in this version is the addition of
MutableImitation, being a slight rewrite/cleanup copypaste of myDeepUnqual. The primary motivation for this over the priorvoid[sizeof], aside from being more GC-friendly, is thatMutableImitationcasts can (sometimes) be evaluated during CTFE, which is necessary for, for instance, passing types toformat.I also entirely ditched the complexities with
refarguments, on the basis that good god I could not care any less about struct copies if I tried. If you're running inner std.algorithm loops with data types with nontrivial copy constructor semantics, your usecases and mine do not overlap in any way.Besides, it can always be added later.