Skip to content

Commit

Permalink
Add a BITS constant in preparation for #76904
Browse files Browse the repository at this point in the history
  • Loading branch information
myrrlyn committed Feb 13, 2021
1 parent 4f259e0 commit b87325b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes will be documented in this file.

This document is written according to the [Keep a Changelog][kac] style.

## 1.2

Add `BITS` in preparation for [`rust-lang/rust#76904`].

## 1.1

Add `leading_ones` and `trailing_ones`, as these methods were introduced in Rust
Expand All @@ -23,3 +27,4 @@ The width relation traits allow users to constrain the widths of numbers they
require.

[kac]: //keepachangelog.com/en/1.0.0/
[`rust-lang/rust#76904`]: https://github.com/rust-lang/rust/issues/76904
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ pub trait IsNumber: Sized
+ RemAssign<Self>
+ for<'a> RemAssign<&'a Self>
{
/// The `[u8; N]` byte array that stores values of `Self`.
type Bytes;

/// The size of this numeric type in bits.
const BITS: u32;

/// Return the memory representation of this number as a byte array in
/// big-endian (network) byte order.
fn to_be_bytes(self) -> Self::Bytes;
Expand Down Expand Up @@ -1294,6 +1298,10 @@ macro_rules! impl_for {
impl IsNumber for $t {
type Bytes = [u8; core::mem::size_of::<Self>()];

// This can be replaced with the `::BITS` inherent once it
// stabilizes in the standard library.
const BITS: u32 = core::mem::size_of::<Self>() as u32 * 8;

func!(to_be_bytes(self) -> Self::Bytes);
func!(to_le_bytes(self) -> Self::Bytes);
func!(to_ne_bytes(self) -> Self::Bytes);
Expand Down

0 comments on commit b87325b

Please sign in to comment.