devela/data/bit/trait.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
// devela::data::bit::trait
//
// TOC
// - definition
// - impls
#[cfg(_bit··)]
use super::Bitwise;
use crate::MismatchedBounds;
#[cfg(doc)]
use crate::MismatchedBounds::{DataOverflow, IndexOutOfBounds, MismatchedIndices};
/// Provides bitwise operations on `T`.
///
/// See also [`Bitwise`] for the equivalent const wrapper.
pub trait BitOps
where
Self: Sized,
{
/// The inner type for the bit representation.
type Inner;
/* new mask */
/// Returns a bitmask of ones from the `[start..=end]` range.
///
/// Sets the rest of the bits to 0.
///
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[doc = include_str!("./benches/mask_range.md")]
#[must_use]
fn bit_mask_range(start: u32, end: u32) -> Self;
/// Returns a bitmask of ones from the `[start..=end]` checked range.
///
/// Sets the rest of the bits to 0.
///
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
#[doc = include_str!("./benches/mask_checked_range.md")]
fn bit_mask_checked_range(start: u32, end: u32) -> Result<Self, MismatchedBounds>;
/* get */
/// Gets the bits in `self` from the `[start..=end]` range.
///
/// Sets the rest of the bits to 0.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_get_range(self, start: u32, end: u32) -> Self;
/// Gets the bits in `self` from the `[start..=end]` checked range.
///
/// Sets the rest of the bits to 0.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_get_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;
/* get value */
/// Gets the rightwards shifted bits in `self` from the `[start..=end]` range.
///
/// Sets the rest of the bits to 0.
///
/// The bits in the specified range are shifted rightwards so that the least
/// significant bit (LSB) aligns with the units place, forming the integer value.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_get_value_range(self, start: u32, end: u32) -> Self;
/// Gets the rightwards shifted bits in `self` from the `[start..=end]` checked range.
///
/// Sets the rest of the bits to 0.
///
/// The bits in the specified range are shifted rightwards so that the least
/// significant bit (LSB) aligns with the units place, forming the integer value.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_get_value_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;
/* set */
/// Sets the bits in `self` to 1 from the `[start..=end]` range.
///
/// Leaves the rest of the bits untouched.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_set_range(self, start: u32, end: u32) -> Self;
/// Sets the bits in `self` to 1 from the `[start..=end]` checked range.
///
/// Leaves the rest of the bits untouched.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_set_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;
/* set value */
/// Sets the given `value` into the bits from the `[start..=end]` range.
///
/// Leaves the rest of the bits unchanged.
///
/// The value is first masked to fit the size of the range, and then
/// it is inserted into the specified bit range of `self`, replacing
/// the existing bits in that range. The rest of the bits in `self` remain unchanged.
/// # Panics
/// Panics if `start >= BITS || end >= BITS || start > end`.
fn bit_set_value_range(self, value: Self::Inner, start: u32, end: u32) -> Self;
/// Sets the given `value` into the bits from the `[start..=end]` checked range.
///
/// Leaves the rest of the bits unchanged.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= BITS || end >= BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_set_value_checked_range(
self,
value: Self::Inner,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds>;
/// Sets the given checked `value` into the bits from the `[start..=end]` checked range.
///
/// Leaves the rest of the bits unchanged.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= BITS || end >= BITS`,
/// [`MismatchedIndices`] if `start > end` and
/// [`DataOverflow`] if `value` does not fit within the specified bit range.
fn bit_set_checked_value_checked_range(
self,
value: Self::Inner,
start: u32,
end: u32,
) -> Result<Self, MismatchedBounds>;
/* unset */
/// Unsets the bits in `self` to 0 from the `[start..=end]` range.
///
/// Leaves the rest of the bits untouched.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_unset_range(self, start: u32, end: u32) -> Self;
/// Unsets the bits in `self` to 0 from the `[start..=end]` checked range.
///
/// Leaves the rest of the bits untouched.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_unset_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;
/* flip */
/// Flips the bits in `self` from the `[start..=end]` range.
///
/// Leaves the rest of the bits untouched.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_flip_range(self, start: u32, end: u32) -> Self;
/// Flips the bits in `self` from the `[start..=end]` checked range.
///
/// Leaves the rest of the bits untouched.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_flip_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;
/* reverse */
/// Reverses the order of the bits in `self` from the `[start..=end]` range.
///
/// Leaves the rest of the bits untouched.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_reverse_range(self, start: u32, end: u32) -> Self;
/// Reverses the order of the bits in `self` from the `[start..=end]` checked range.
///
/// Leaves the rest of the bits untouched.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_reverse_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds>;
/* count */
/// Counts the number of 1s in `bits` from the `[start..=end]` range.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_count_ones_range(self, start: u32, end: u32) -> u32;
/// Counts the number of 1s in `bits` from the `[start..=end]` checked range.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_count_ones_checked_range(self, start: u32, end: u32) -> Result<u32, MismatchedBounds>;
/// Counts the number of 0s in `bits` from the `[start..=end]` range.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_count_zeros_range(self, start: u32, end: u32) -> u32;
/// Counts the number of 0s in `bits` from the `[start..=end]` checked range.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_count_zeros_checked_range(self, start: u32, end: u32) -> Result<u32, MismatchedBounds>;
/* find first */
/// Finds the index of the first 1 in `bits` from the `[start..=end]` range.
///
/// Returns `None` if there are no bits set.
///
/// The index is relative to the entire sequence of `bits`, not to the given `start`.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_find_first_one_range(self, start: u32, end: u32) -> Option<u32>;
/// Finds the index of the first 1 in `bits` from the `[start..=end]` checked range.
///
/// Returns `None` if there are no bits set.
///
/// The index is relative to the entire sequence of `bits`, not to the given `start`.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_find_first_one_checked_range(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds>;
/// Finds the index of the first 0 in `bits` from the `[start..=end]` range.
///
/// Returns `None` if there are no bits unset.
///
/// The index is relative to the entire sequence of `bits`, not to the given `start`.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_find_first_zero_range(self, start: u32, end: u32) -> Option<u32>;
/// Finds the index of the first 0 in `bits` from the `[start..=end]` checked range.
///
/// Returns `None` if there are no bits unset.
///
/// The index is relative to the entire sequence of `bits`, not to the given `start`.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_find_first_zero_checked_range(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds>;
/* find last */
/// Finds the index of the last 1 in `bits` from the `[start..=end]` range.
///
/// Returns `None` if there are no bits set.
///
/// The index is relative to the entire sequence of `bits`, not to the given `start`.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_find_last_one_range(self, start: u32, end: u32) -> Option<u32>;
/// Finds the index of the last 1 in `bits` from the `[start..=end]` checked range.
///
/// Returns `None` if there are no bits set.
///
/// The index is relative to the entire sequence of `bits`, not to the given `start`.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_find_last_one_checked_range(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds>;
/// Finds the index of the last 0 in `bits` from the `[start..=end]` range.
///
/// Returns `None` if there are no bits unset.
///
/// The index is relative to the entire sequence of `bits`, not to the given `start`.
/// # Panics
/// Panics in debug if `start >= Self::BITS` || `end >= Self::BITS` || `start > end`.
#[must_use]
fn bit_find_last_zero_range(self, start: u32, end: u32) -> Option<u32>;
/// Finds the index of the last 0 in `bits` from the `[start..=end]` checked range.
///
/// Returns `None` if there are no bits unset.
///
/// The index is relative to the entire sequence of `bits`, not to the given `start`.
/// # Errors
/// Returns [`IndexOutOfBounds`] if `start >= Self::BITS` || `end >= Self::BITS`
/// and [`MismatchedIndices`] if `start > end`.
fn bit_find_last_zero_checked_range(
self,
start: u32,
end: u32,
) -> Result<Option<u32>, MismatchedBounds>;
}
macro_rules! impl_bit_ops {
() => {
impl_bit_ops![
i8:"_bit_i8", i16:"_bit_i16", i32:"_bit_i32",
i64:"_bit_i64", i128:"_bit_i128", isize:"_bit_isize",
u8:"_bit_u8", u16:"_bit_u16", u32:"_bit_u32",
u64:"_bit_u64", u128:"_bit_u128", usize:"_bit_usize"
];
};
// `$t`: the type to implement the trait for.
// $cap: the capability feature that enables the given implementation. E.g "_bit_u8".
($($t:ty : $cap:literal),+) => { $( impl_bit_ops![@$t:$cap]; )+ };
(@$t:ty : $cap:literal) => {
#[cfg(feature = $cap )]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
impl BitOps for $t {
type Inner = $t;
// new mask
fn bit_mask_range(start: u32, end: u32) -> Self {
Bitwise::<$t>::mask_range(start, end).0
}
fn bit_mask_checked_range(start: u32, end: u32) -> Result<Self, MismatchedBounds> {
Ok(Bitwise::<$t>::mask_checked_range(start, end)?.0)
}
// get
fn bit_get_range(self, start: u32, end: u32) -> Self {
Bitwise(self).get_range(start, end).0
}
fn bit_get_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds> {
Ok(Bitwise(self).get_checked_range(start, end)?.0)
}
// get value
fn bit_get_value_range(self, start: u32, end: u32) -> Self {
Bitwise(self).get_value_range(start, end).0
}
fn bit_get_value_checked_range(self, start: u32, end: u32)
-> Result<Self, MismatchedBounds> {
Ok(Bitwise(self).get_value_checked_range(start, end)?.0)
}
// set
fn bit_set_range(self, start: u32, end: u32) -> Self {
Bitwise(self).set_range(start, end).0
}
fn bit_set_checked_range(self, start: u32, end: u32) -> Result<Self, MismatchedBounds> {
Ok(Bitwise(self).set_checked_range(start, end)?.0)
}
// set value
fn bit_set_value_range(self, value: Self::Inner, start: u32, end: u32) -> Self {
Bitwise(self).set_value_range(value, start, end).0
}
fn bit_set_value_checked_range(self, value: Self::Inner, start: u32, end: u32)
-> Result<Self, MismatchedBounds> {
Ok(Bitwise(self).set_value_checked_range(value, start, end)?.0)
}
fn bit_set_checked_value_checked_range(self, value: Self::Inner, start: u32, end: u32)
-> Result<Self, MismatchedBounds> {
Ok(Bitwise(self).set_checked_value_checked_range(value, start, end)?.0)
}
// unset
fn bit_unset_range(self, start: u32, end: u32) -> Self {
Bitwise(self).unset_range(start, end).0
}
fn bit_unset_checked_range(self, start: u32, end: u32)
-> Result<Self, MismatchedBounds> {
Ok(Bitwise(self).unset_checked_range(start, end)?.0)
}
// flip
fn bit_flip_range(self, start: u32, end: u32) -> Self {
Bitwise(self).flip_range(start, end).0
}
fn bit_flip_checked_range(self, start: u32, end: u32)
-> Result<Self, MismatchedBounds> {
Ok(Bitwise(self).flip_checked_range(start, end)?.0)
}
// reverse
fn bit_reverse_range(self, start: u32, end: u32) -> Self {
Bitwise(self).reverse_range(start, end).0
}
fn bit_reverse_checked_range(self, start: u32, end: u32)
-> Result<Self, MismatchedBounds> {
Ok(Bitwise(self).reverse_checked_range(start, end)?.0)
}
// count
fn bit_count_ones_range(self, start: u32, end: u32) -> u32 {
Bitwise(self).count_ones_range(start, end)
}
fn bit_count_ones_checked_range(self, start: u32, end: u32)
-> Result<u32, MismatchedBounds> {
Bitwise(self).count_ones_checked_range(start, end)
}
fn bit_count_zeros_range(self, start: u32, end: u32) -> u32 {
Bitwise(self).count_zeros_range(start, end)
}
fn bit_count_zeros_checked_range(self, start: u32, end: u32)
-> Result<u32, MismatchedBounds> {
Bitwise(self).count_zeros_checked_range(start, end)
}
// find first
fn bit_find_first_one_range(self, start: u32, end: u32) -> Option<u32> {
Bitwise(self).find_first_one_range(start, end)
}
fn bit_find_first_one_checked_range(self, start: u32, end: u32)
-> Result<Option<u32>, MismatchedBounds> {
Bitwise(self).find_first_one_checked_range(start, end)
}
fn bit_find_first_zero_range(self, start: u32, end: u32) -> Option<u32> {
Bitwise(self).find_first_zero_range(start, end)
}
fn bit_find_first_zero_checked_range(self, start: u32, end: u32)
-> Result<Option<u32>, MismatchedBounds> {
Bitwise(self).find_first_zero_checked_range(start, end)
}
// find last
fn bit_find_last_one_range(self, start: u32, end: u32) -> Option<u32> {
Bitwise(self).find_last_one_range(start, end)
}
fn bit_find_last_one_checked_range(self, start: u32, end: u32)
-> Result<Option<u32>, MismatchedBounds> {
Bitwise(self).find_last_one_checked_range(start, end)
}
fn bit_find_last_zero_range(self, start: u32, end: u32) -> Option<u32> {
Bitwise(self).find_last_zero_range(start, end)
}
fn bit_find_last_zero_checked_range(self, start: u32, end: u32)
-> Result<Option<u32>, MismatchedBounds> {
Bitwise(self).find_last_zero_checked_range(start, end)
}
}
};
}
impl_bit_ops![];