add IS_SIGNED associated constant

This commit is contained in:
Trevor Spiteri 2021-03-20 15:41:50 +01:00
parent 9b3475d59e
commit 6187d714ab
6 changed files with 71 additions and 9 deletions

View File

@ -86,9 +86,10 @@ The conversions supported cover the following cases.
* [`next_power_of_two`][f-npot-1-7],
[`checked_next_power_of_two`][f-cnpot-1-7],
[`wrapping_next_power_of_two`][f-wnpot-1-7]
* The following methods were added to all fixed-point numbers, to
the [`Fixed`][tf-1-7] trait, and to the [`Wrapping`][w-1-7] and
[`Unwrapped`][u-1-7] wrappers:
* The following constant and methods were added to all fixed-point
numbers, to the [`Fixed`][tf-1-7] trait, and to the
[`Wrapping`][w-1-7] and [`Unwrapped`][u-1-7] wrappers:
* [`IS_SIGNED`][f-is-1-7]
* [`from_be`][f-fb-1-7], [`from_le`][f-fl-1-7]
* [`to_be`][f-tb-1-7], [`to_le`][f-tl-1-7]
* [`swap_bytes`][f-sb-1-7]
@ -110,7 +111,7 @@ The conversions supported cover the following cases.
signed numbers.
* The following constants, which are available in other programming
language standard libraries, were added to the [`consts`][c-1-7]
module and as associated constants for fixed-point types:
module and as associated constants to fixed-point types:
* [`SQRT_PI`][c-rp-1-7] (Go), [`FRAC_1_SQRT_PI`][c-1rp-1-7] (C++)
* [`SQRT_3`][c-r3-1-7] (C++), [`FRAC_1_SQRT_3`][c-1r3-1-7] (C++)
* [`SQRT_E`][c-re-1-7] (Go)
@ -148,6 +149,7 @@ The conversions supported cover the following cases.
[f-fl-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.from_le
[f-flb-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.from_le_bytes
[f-fnb-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.from_ne_bytes
[f-is-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.IS_SIGNED
[f-m-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.midpoint
[f-npot-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedU32.html#method.next_power_of_two
[f-rb-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.reverse_bits

View File

@ -13,9 +13,10 @@ Version 1.7.0 (unreleased)
* [`next_power_of_two`][f-npot-1-7],
[`checked_next_power_of_two`][f-cnpot-1-7],
[`wrapping_next_power_of_two`][f-wnpot-1-7]
* The following methods were added to all fixed-point numbers, to
the [`Fixed`][tf-1-7] trait, and to the [`Wrapping`][w-1-7] and
[`Unwrapped`][u-1-7] wrappers:
* The following constant and methods were added to all fixed-point
numbers, to the [`Fixed`][tf-1-7] trait, and to the
[`Wrapping`][w-1-7] and [`Unwrapped`][u-1-7] wrappers:
* [`IS_SIGNED`][f-is-1-7]
* [`from_be`][f-fb-1-7], [`from_le`][f-fl-1-7]
* [`to_be`][f-tb-1-7], [`to_le`][f-tl-1-7]
* [`swap_bytes`][f-sb-1-7]
@ -37,7 +38,7 @@ Version 1.7.0 (unreleased)
signed numbers.
* The following constants, which are available in other programming
language standard libraries, were added to the [`consts`][c-1-7]
module and as associated constants for fixed-point types:
module and as associated constants to fixed-point types:
* [`SQRT_PI`][c-rp-1-7] (Go), [`FRAC_1_SQRT_PI`][c-1rp-1-7] (C++)
* [`SQRT_3`][c-r3-1-7] (C++), [`FRAC_1_SQRT_3`][c-1r3-1-7] (C++)
* [`SQRT_E`][c-re-1-7] (Go)
@ -73,6 +74,7 @@ Version 1.7.0 (unreleased)
[f-fl-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.from_le
[f-flb-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.from_le_bytes
[f-fnb-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.from_ne_bytes
[f-is-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.IS_SIGNED
[f-m-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.midpoint
[f-npot-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedU32.html#method.next_power_of_two
[f-rb-1-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.reverse_bits
@ -279,7 +281,7 @@ Version 0.5.7 (2020-05-11)
to the `FixedUnsigned` trait:
* `wrapping_next_power_of_two`
* The `PHI` and `FRAC_1_PHI` constants were added to the `consts`
module and as associated constants for fixed-point types.
module and as associated constants to fixed-point types.
Version 0.5.6 (2020-05-01)
==========================

View File

@ -51,6 +51,26 @@ assert_eq!(Fix::MAX, Fix::from_bits(", $s_inner, "::MAX));
pub const MAX: $Fixed<Frac> = Self::from_bits(<$Inner>::MAX);
}
comment! {
if_signed_unsigned!($Signedness, "[`true`]", "[`false`]"),
"[`bool`] because the [`", $s_fixed, "`] type is ",
if_signed_unsigned!($Signedness, "signed", "unsigned"),
".
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert!(", if_signed_unsigned!($Signedness, "", "!"), "Fix::IS_SIGNED);
```
[`", $s_fixed, "`]: #
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
pub const IS_SIGNED: bool = if_signed_unsigned!($Signedness, true, false);
}
comment! {
"Creates a fixed-point number that has a bitwise
representation identical to the given integer.

View File

@ -358,6 +358,11 @@ where
/// The largest value that can be represented.
const MAX: Self;
/// [`true`][`bool`] if the type is signed.
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
const IS_SIGNED: bool;
/// The number of integer bits.
const INT_NBITS: u32;
@ -2382,6 +2387,7 @@ macro_rules! impl_fixed {
type Frac = Frac;
const MIN: Self = Self::MIN;
const MAX: Self = Self::MAX;
const IS_SIGNED: bool = Self::IS_SIGNED;
const INT_NBITS: u32 = Self::INT_NBITS;
const FRAC_NBITS: u32 = Self::FRAC_NBITS;
trait_delegate! { fn from_bits(bits: Self::Bits) -> Self }

View File

@ -72,6 +72,22 @@ impl<F: Fixed> Unwrapped<F> {
/// ```
pub const MAX: Unwrapped<F> = Unwrapped(F::MAX);
/// [`true`][`bool`] if the type is signed.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I16F16, U16F16},
/// Unwrapped,
/// };
/// assert!(Unwrapped::<I16F16>::IS_SIGNED);
/// assert!(!Unwrapped::<U16F16>::IS_SIGNED);
/// ```
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
pub const IS_SIGNED: bool = F::IS_SIGNED;
/// The number of integer bits.
///
/// # Examples

View File

@ -69,6 +69,22 @@ impl<F: Fixed> Wrapping<F> {
/// ```
pub const MAX: Wrapping<F> = Wrapping(F::MAX);
/// [`true`][`bool`] if the type is signed.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I16F16, U16F16},
/// Wrapping,
/// };
/// assert!(Wrapping::<I16F16>::IS_SIGNED);
/// assert!(!Wrapping::<U16F16>::IS_SIGNED);
/// ```
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
pub const IS_SIGNED: bool = F::IS_SIGNED;
/// The number of integer bits.
///
/// # Examples