-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider creating a safe version of this library #10
Comments
What advantage would this have over the status quo? Every use of Any option that is present would necessarily duplicate nearly the entire crate, as |
This is probably a philosophical argument where we just agree to disagree - but I'll answer shortly anyway. Rust attempts to bring safety to programming, so undefined behaviour would be constrained. There are obviously still bugs in the Rust runtime which cause undefined behaviour, but those are rare and getting even more rare. Any library that uses I don't consider On the code itself, I don't think the duplication is as big as you think it is, having already looked at all the unsafe usages. Especially with a couple helper methods, I believe the duplication could be just a few lines of code. I can even make the pull myself if you want – but I have no wish to force the issue if you don't think it is worthwhile. And on the compiler optimizations, yes, any non-static value is unknowable to the compiler, but that doesn't mean the compiler can't reason about it. For example if you do In any case, totally up to you – if you don't feel this is worthwhile especially on a proof-of-concept implementation such as this, then somebody else will fill that niche. |
I suspected you were going for a philosophical view of things going in, but I wasn't positive.
At the risk of an argument from authority, I am. By even using the Rust compiler and standard library, you're using code that I've worked on.
By "safe", I presume you mean "sound". This is where debug assertions come in, which are widespread as I mentioned. Without looking over the full crate right now, I can't think of a case where there isn't a debug assertion present for something that could result in UB.
It's both. I spun this out of my work on the
Perhaps within
Array indexing uses
Happy to be proven wrong! Every time a ranged integer is constructed,
I'm not going to promise a PR gets merged, of course, but if it is a small diff and would require minimal effort to maintain, including for future code, then I am open to the idea. My ultimate goal is to get something like this as part of Rust itself, where |
It's probably pointless for me to reiterate my position (and I don't mean that in a bad way!) as you know all my arguments better than I know them myself. However, I will try to summarize as clearly as I can:
If this library is meant to be included in the Rust standard library itself, I have no complaints about the use of unsafe. But if this library is meant to be used by other projects directly, I'd hope to reduce the amount of unsafe. I wrote a pull request that basically changes most usages of The pull isn't polished yet, as I don't want to work on something that will not get merged, so I wanted your opinion on it at this point. As a follow up after that pull, there are exactly four remaining usages of "internal" unsafe (safe functions which contain an unsafe block):
To make those usages safe, the idea would be to add a feature flag Possibly |
Could you possibly consider making a safe version of this library which would not use any
unsafe
constructs internally?It's obvious that some compiler optimizations will be lost and unneeded range checks will be added to some places. But in some cases it is also possible to prove that the value is in the range to the compiler, while still not having explicit panics generated. And in many cases compiler optimizations while inlining will remove any superfluous range checks.
In theory, it could even be a cargo feature to enable
no_internal_unsafe
while keeping the same API.To be clear, I'm fine with having
unsafe
functions in the API as calling code needs to be also marked unsafe - but implicitly usingunsafe
blocks inside functions requires trusting the correct implementation of all of those functions, in this version and in any future versions.The text was updated successfully, but these errors were encountered: