-
Notifications
You must be signed in to change notification settings - Fork 22
ACP: Negation functions for NonZeroI*
#105
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Proposed implementation: jmillikin/upstream__rust@a807a2d Docs screenshots: |
How about |
It's easy to implement, but my understanding is that trait impls for two stable types can't be marked #[unstable(feature = "nonzero_negation_ops", issue = "none")]
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Neg for $Ty {
type Output = Self;
#[inline]
fn neg(self) -> Self {
unsafe { $Ty::new_unchecked(Neg::neg(self.get())) }
}
}
|
That's not a deal-breaker if the trait implementation is sensible/desirable. |
I agree that |
Given that the signed nonzero types already have things like Trait impls can't go in unstable, though, so that at least should be a different PR, and I'll leave it for a real libs-api member to comment on it. |
OK, I've filed PR rust-lang/rust#102341 to implement To avoid merge conflicts, I'll send a second PR for the negation functions once that first one lands. |
Traits take at least 10 days for FCP, so that PR will take much longer to land than one for unstable inherent methods. |
Interesting. OK, filed rust-lang/rust#102342 to add the new methods. Once one of them merges I'll rebase the second one. |
…tmcm Add negation methods for signed non-zero integers. Performing negation with defined wrapping semantics (such as `wrapping_neg()`) on a non-zero integer currently requires unpacking to a primitive and re-wrapping. Since negation of non-zero signed integers always produces a non-zero result, it is safe to implement the various `*_neg()` methods for `NonZeroI{N}`. I'm not sure what to do about the `#[unstable(..., issue = "none")]` here -- should I file a tracking issue, or is that handled by the Rust dev team? ACP: rust-lang/libs-team#105
…tmcm Add negation methods for signed non-zero integers. Performing negation with defined wrapping semantics (such as `wrapping_neg()`) on a non-zero integer currently requires unpacking to a primitive and re-wrapping. Since negation of non-zero signed integers always produces a non-zero result, it is safe to implement the various `*_neg()` methods for `NonZeroI{N}`. I'm not sure what to do about the `#[unstable(..., issue = "none")]` here -- should I file a tracking issue, or is that handled by the Rust dev team? ACP: rust-lang/libs-team#105
Add negation methods for signed non-zero integers. Performing negation with defined wrapping semantics (such as `wrapping_neg()`) on a non-zero integer currently requires unpacking to a primitive and re-wrapping. Since negation of non-zero signed integers always produces a non-zero result, it is safe to implement the various `*_neg()` methods for `NonZeroI{N}`. I'm not sure what to do about the `#[unstable(..., issue = "none")]` here -- should I file a tracking issue, or is that handled by the Rust dev team? ACP: rust-lang/libs-team#105
Implement `Neg` for signed non-zero integers. Negating a non-zero integer currently requires unpacking to a primitive and re-wrapping. Since negation of non-zero signed integers always produces a non-zero result, it is safe to implement `Neg` for `NonZeroI{N}`. The new `impl` is marked as stable because trait impls for two stable types can't be marked unstable. See discussion on rust-lang/libs-team#105 for additional context.
…tolnay Add `is_positive` method for signed non-zero integers. ACP: rust-lang/libs-team#105
Stabilize feature `nonzero_negation_ops` Fixes rust-lang#102443 ACP: rust-lang/libs-team#105
Stabilize feature `nonzero_negation_ops` Fixes #102443 ACP: rust-lang/libs-team#105
Add `is_positive` method for signed non-zero integers. ACP: rust-lang/libs-team#105
Stabilize feature `nonzero_negation_ops` Fixes #102443 ACP: rust-lang/libs-team#105
Stabilize feature `nonzero_negation_ops` Fixes #102443 ACP: rust-lang/libs-team#105
Stabilize feature `nonzero_negation_ops` Fixes #102443 ACP: rust-lang/libs-team#105
Uh oh!
There was an error while loading. Please reload this page.
Proposal
Problem statement
Integer primitives have functions such as
overflowing_neg()
for numeric negation with controlled overflow. These functions are currently missing from thecore::num::NonZeroI{8,16,32,64}
types.Motivation, use-cases
Non-zero integers are frequently used to represent error codes from low-level OS APIs, because these APIs use
0
to indicate non-error. Some APIs use signed error codes as a form of namespacing, such as internal/public (FreeBSD) or kernel/userspace (Linux).For example, the FUSE protocol uses userspace error codes (positive non-zero) but negates them when sending an error frame to the kernel.
Solution sketches
Since the primitive integer types already have
*_neg()
functions, and negation can't return zero for non-zero inputs, writing wrappers for the non-zero integers is straightforward.Links and related work
rust-lang/rust#89065
rust-lang/rust#84186
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: