Add mechanism for pointers that prevents dereferencing#1592
Add mechanism for pointers that prevents dereferencing#1592schveiguy wants to merge 4 commits intodlang:masterfrom
Conversation
like pointers for pointer comparison and math. All usable with @safe code.
src/core/internal/array.d
Outdated
|
|
||
| /* | ||
| * This allows safe use of a pointer without dereferencing. Useful when you | ||
| * want to declare that all you care about it the pointer value itself. Keeps |
|
Stupid windows makefiles... |
| * Use this if you are in system code and wish to get back to unsafe | ||
| * pointer-land. | ||
| */ | ||
| inout(T) *ptr() inout @system |
There was a problem hiding this comment.
I think this shouldn't be inout since your returning a copy of the pointer and if the user wants a const pointer, then PtrVal!(const T*) should be used.
There was a problem hiding this comment.
You misunderstand why this is inout. If you have a const(PtrVal!(...)), then you can't call ptr on it unless the function is marked const or inout. I'd need several copies of the same function. This is the whole point of inout.
|
@schveiguy maybe I didn't explain well, but I expected to see something like this: Why did you decide to instead templatize on the pointer target type and not the pointer type itself? |
|
In other words, I think it's more straightforward for the users (those that don't use |
|
Isn't this pretty easy? alias PtrValFor(T) = PtrVal!(typeof(*T.init));In any case, I look at it much differently. When I want to say "pointer to int", I write The code is much more straightforward, there are no template constraints because you can't write an incorrect Regarding the
My way is much simpler. Yes, it makes for issues when you want to rebind const pointers. But I don't see this being a tremendously difficult issue to deal with. If we ever get proper tail-modifier support, this problem goes away. It may be that if we add this mechanism to the language (and not just in core/internal), then we can revisit fixing some corner cases. |
|
Yes, I understand that you're comming from the type constructor perspective ( About the specific points you raised:
In the end, it all comes down to the question: Is this value type, or pointer type in a safe disguise? Currently it looks like you have more of a SafePtr, than PtrVal. |
src/core/internal/array.d
Outdated
| * Cast to void pointer type. TODO: see if there is a way to make this | ||
| * implicit. | ||
| */ | ||
| auto toVoid() inout @trusted |
There was a problem hiding this comment.
BTW, you can make this a template (with no params) to make the import optional.
One thing about this is that you can get the pointer back. It can't be a value type (and actually, in order to be properly scanned by the GC, it must have a pointer internally).
As opposed to:
I'm not sure why this is a benefit.
Shouldn't this work? With your proposed usage, I'd think you'd have to template string x;
char[] y;
auto d = x.ptrval - y.ptrval;This is a pretty small type, perhaps the easiest thing to do is for you to post what you think the code should be, and we can discuss the differences. I'm certainly willing to listen to a different point of view, but I want to know exactly what you are talking about. Keep in mind that the main purpose of this type is to allow a drop-in replacement for |
|
somewhat related: dlang/phobos#6231 |
|
I had forgotten about this. I'm going to just close it, as I don't think it will be merged. |
Inspired by @WalterBright's changes for
@safeaccess toarr.ptr, this attempts to make a pseudo-pointer type that does everything a pointer can do, except dereference, and create a pointer at a relative location.The goal is to have something that acts like a pointer (i.e. can compare, subtraction yields signed result), but is completely
@safe. It also provides a mechanism to get back to the pointer, but only in@systemcode. There are many times you don't care at all about what a pointer points at, but just where it is in memory. Or you want to enforce this rule for@safecode (which does allow dereferencing normal pointers), or even@systemcode.See #1590 and dlang/phobos#4427
Note: I put this in core.internal for now, but I'm hoping we can move it to
object.das a general mechanism. All names up for debate.Some issues with this code:
void *equivalentPtrVal, you have to usetoVoidconversion function (no language support for this)PtrVal!(const(int))but instead opted forconst(PtrVal!int). This makes things much more sane, but there is no tail-const support because of this.