-
Notifications
You must be signed in to change notification settings - Fork 136
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
Define CheckedAdd
and CheckedSub
traits
#293
Comments
Alternatively, num::CheckedAdd looks to have been implemented with a slightly different interface, but can provide something similar if the additional dependency is okay. I'd be happy to draft up an example PR for either if this is an agreeable change. |
I would definitely prefer using an existing trait definition rather than providing our own. I don't think depending on I do think that we should have the same/similar traits implemented for all our "address" types:
Here's a list of all the traits we either do implement or should implement:
There is also the question of conversions. This was brought up in #268. Right now we don't implement any, here is what we could do:
@phil-opp @ncatelli @budde25 which of these traits do you think we should implement? |
I've drafted the above PR, #298, with an initial implementation of checked ops with an example applied to |
I would treat them as normal unsigned integers (with a gap in the middle). So upper half address > lower half address and treat the
I think one problem with implementing it for both
These all seem reasonable. Yes, I think that we can assume that references are canonical on x86_64 in general. The question is how we want to treat the upcoming 5-level paging? We could panic on conversion in that case, but I'm not sure if panicking in From impls is encouraged.
Not sure if I like these ones because there are multiple ways to do these conversions. For example, for the |
How would we want to handle an arbitrary range that crossed the gap? Also have it be empty? Automatically jump the gap? Panic?
So I think this is also a problem for the
Personally, I think the panic is fine (for now). Before we (potentially) support 5-level paging, we can just have a disclaimer that is like: "if you use 5-level paging, some methods might panic, but safety will not be violated".
Agreed, that's a very good point. Especially because we already have methods for these conversions, we don't need to implement |
Good question. Given that the methods of the We still need to discuss how this should interact with the Add/Sub operations, though. Right now, we're kind of using For Whatever we decide, I think it is important that our implementations are consistent with each other: If our
I didn't remember that we already have an Add impl for usize. Does it lead to problems in practice? |
It means you can't write code like the following: let addr1 = PhysAddr::new(0x52);
let addr2 = addr1 + 3; although you get get a reasonable compiler error:
I think having the |
This removes `Add`/`AddAssign`/`Sub`/`SubAssign` with `usize` from `VirtAddr` and `PhysAddr` (which are always 64-bit, even on 32-bit platforms). This makes it possible to write code like: ```rust let addr1 = PhysAddr::new(0x52); let addr2 = addr1 + 3; ``` without getting a "multiple `impl`s" compiler error. This is essentially the breaking change mentioned in #293 (comment) Signed-off-by: Joe Richey <[email protected]>
This removes `Add`/`AddAssign`/`Sub`/`SubAssign` with `usize` from `VirtAddr` and `PhysAddr` (which are always 64-bit, even on 32-bit platforms). This makes it possible to write code like: ```rust let addr1 = PhysAddr::new(0x52); let addr2 = addr1 + 3; ``` without getting a "multiple `impl`s" compiler error. This is essentially the breaking change mentioned in #293 (comment) Signed-off-by: Joe Richey <[email protected]>
This removes `Add`/`AddAssign`/`Sub`/`SubAssign` with `usize` from `VirtAddr` and `PhysAddr` (which are always 64-bit, even on 32-bit platforms). This makes it possible to write code like: ```rust let addr1 = PhysAddr::new(0x52); let addr2 = addr1 + 3; ``` without getting a "multiple `impl`s" compiler error. This is essentially the breaking change mentioned in rust-osdev/x86_64#293 (comment) Signed-off-by: Joe Richey <[email protected]>
There are a few places where I've seen, or they could benefit from a,
checked_add
andchecked_sub
implementations throughout the library, such as onVirtAddr
,PhysFrame
,PhysAddr
... etc. I would like to define aCheckedAdd
andCheckedSub
trait similar to thestd::ops::Add
trait that defines a checked_* method.I would like to add this specifically to provide for checked_operations across multiple
Rhs
types. An example being aCheckedAdd<Rhs=PhysFrame> for PhysFrame
andCheckedAdd<Rhs=PhysAddr> for PhysFrame
.The text was updated successfully, but these errors were encountered: