This code is always inlined to avoid a build error if the error path of
`build_assert` cannot be optimized out. Add a comment justifying the
`#[inline(always)]` property to avoid it being taken away by mistake.
Reviewed-by: Daniel Almeida <[email protected]>
Signed-off-by: Alexandre Courbot <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
The `Bounded::__new()` constructor relies on the caller to ensure the
value can be represented within N bits. Failing to uphold this
requirement breaks the type invariant. Mark it as unsafe and document
this requirement in a Safety section to make the contract explicit.
Update all call sites to use unsafe blocks and change their comments
from `INVARIANT:` to `SAFETY:`, as they are now justifying unsafe
operations rather than establishing type invariants.
Fixes: 01e345e82e ("rust: num: add Bounded integer wrapping type")
Link: https://lore.kernel.org/all/[email protected]/
Reported-by: Miguel Ojeda <[email protected]>
Closes: https://github.com/Rust-for-Linux/linux/issues/1211
Signed-off-by: Hsiu Che Yu <[email protected]>
Acked-by: Alexandre Courbot <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
`from_expr` relies on `build_assert` to infer that the passed expression
fits the type's boundaries at build time. That inference can only be
successful its code (and that of `fits_within`, which performs the
check) is inlined, as a dedicated function would need to work with a
variable and cannot verify that property.
While inlining happens as expected in most cases, it is not guaranteed.
In particular, kernel options that optimize for size like
`CONFIG_CC_OPTIMIZE_FOR_SIZE` can result in `from_expr` not being
inlined.
Add `#[inline(always)]` attributes to both `fits_within` and `from_expr`
to make the compiler inline these functions more aggressively, as it
does not make sense to use them non-inlined anyway.
[ For reference, the errors look like:
ld.lld: error: undefined symbol: rust_build_error
>>> referenced by build_assert.rs:83 (rust/kernel/build_assert.rs:83)
>>> rust/doctests_kernel_generated.o:(<kernel::num::bounded::Bounded<u8, 1>>::from_expr) in archive vmlinux.a
- Miguel ]
Fixes: 01e345e82e ("rust: num: add Bounded integer wrapping type")
Reported-by: kernel test robot <[email protected]>
Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
Suggested-by: Gary Guo <[email protected]>
Signed-off-by: Alexandre Courbot <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
Add the `Bounded` integer wrapper type, which restricts the number of
bits allowed to represent of value.
This is useful to e.g. enforce guarantees when working with bitfields
that have an arbitrary number of bits.
Alongside this type, provide many `From` and `TryFrom` implementations
are to reduce friction when using with regular integer types. Proxy
implementations of common integer operations are also provided.
Signed-off-by: Alexandre Courbot <[email protected]>
Reviewed-by: Alice Ryhl <[email protected]>
Link: https://patch.msgid.link/[email protected]
[ Added intra-doc link. Fixed a few other nits. - Miguel ]
Signed-off-by: Miguel Ojeda <[email protected]>