Expand description
Helpers for performing lossless integer casts.
The as keyword can be used to perform casts between integer types, but it unfortunately makes
no distinction between casts that are lossless, and casts from a larger type into a smaller one
that might silently strip data away. Thus, its use in the kernel is discouraged in favor of
From implementations.
Conversely, there are casts that are lossless depending on the build architecture (such as
casting usize to u64 on 32 or 64 bit archs), but not supported by From
implementations in the standard library because they are not portable. It does however make
sense for the kernel to support these, if only for code that is architecture-specific.
This module provides ways to perform such conversions safely:
- A series of const functions (e.g.
usize_as_u64) supporting safe conversions in const context. Conversions supported byFromimplementations in the standard library are also covered as theFromtrait cannot be used in const context. - Two extension traits,
FromSafeCastandIntoSafeCast, providing conversion methods similar toFromandIntofor conversions that are safe to perform in the kernel, but not supported by the standard library. - Another series of const functions (e.g.
u64_into_u8) supporting the conversion of a const value from a larger type into a smaller one, provided the value fits into the destination type. This is useful if a constant is defined as a larger type, but needs to be used as a smaller one. - An
archsub-module, defining more conversion functions that are only guaranteed to be lossless for a given pointer size. These can only be used in code that is specific to a given pointer size.
§Examples
use kernel::num::casts::{self, FromSafeCast, IntoSafeCast};
// Conversion from const context.
const USIZED_CONST: usize = casts::u8_as_usize(255u8);
// Non-const conversions.
let a = u64::from_safe_cast(4096usize);
let b: u64 = 4096usize.into_safe_cast();Modules§
- arch
- Conversions that are only lossless for the current architecture.
Traits§
- From
Safe Cast - Extension trait providing guaranteed lossless cast to
SelffromT. - Into
Safe Cast - Counterpart to the
FromSafeCasttrait, i.e. this trait is toFromSafeCastwhatIntois toFrom.
Functions§
- u8_
as_ u16 - Losslessly converts a
u8into au16. - u8_
as_ u32 - Losslessly converts a
u8into au32. - u8_
as_ u64 - Losslessly converts a
u8into au64. - u8_
as_ usize - Losslessly converts a
u8into ausize. - u16_
as_ u32 - Losslessly converts a
u16into au32. - u16_
as_ u64 - Losslessly converts a
u16into au64. - u16_
as_ usize - Losslessly converts a
u16into ausize. - u16_
into_ u8 - Performs a build-time safe conversion of a
u16constant value into au8. - u32_
as_ u64 - Losslessly converts a
u32into au64. - u32_
as_ usize - Losslessly converts a
u32into ausize. - u32_
into_ u8 - Performs a build-time safe conversion of a
u32constant value into au8. - u32_
into_ u16 - Performs a build-time safe conversion of a
u32constant value into au16. - u64_
into_ u8 - Performs a build-time safe conversion of a
u64constant value into au8. - u64_
into_ u16 - Performs a build-time safe conversion of a
u64constant value into au16. - u64_
into_ u32 - Performs a build-time safe conversion of a
u64constant value into au32. - usize_
as_ u64 - Losslessly converts a
usizeinto au64. - usize_
into_ u8 - Performs a build-time safe conversion of a
usizeconstant value into au8. - usize_
into_ u16 - Performs a build-time safe conversion of a
usizeconstant value into au16. - usize_
into_ u32 - Performs a build-time safe conversion of a
usizeconstant value into au32.