Skip to content

Commit 0adf011

Browse files
authored
Merge pull request #122 from kpreid/tobytes
Add implementations of `num_traits::{FromBytes, ToBytes}`.
2 parents 0bebc34 + db1940e commit 0adf011

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212

1313
### Added
1414
- Added support for `arbitrary` crate. Fixes [#110]. By [@FL33TW00D].
15+
- New `num-traits` implementations: `FromBytes` and `ToBytes` for `f16` and `bf16`. By [@kpreid].
1516

1617
### Fixed
1718
- Suppressed unexpected_cfg lint warnings on newer versions of stable Rust.
@@ -364,6 +365,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
364365
[@eiz]: https://github.com/eiz
365366
[@comath]: https://github.com/comath
366367
[@FL33TW00D]: https://github.com/FL33TW00D
368+
[@kpreid]: https://github.com/kpreid
367369

368370

369371
[Unreleased]: https://github.com/starkat99/half-rs/compare/v2.4.1...HEAD

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ See the [crate documentation](https://docs.rs/half/) for more details.
3838
- **`serde`** - Implement `Serialize` and `Deserialize` traits for `f16` and `bf16`. This adds a
3939
dependency on the [`serde`](https://crates.io/crates/serde) crate.
4040

41-
- **`num-traits`** — Enable `ToPrimitive`, `FromPrimitive`, `Num`, `Float`, `FloatCore` and
42-
`Bounded` trait implementations from the [`num-traits`](https://crates.io/crates/num-traits) crate.
41+
- **`num-traits`** — Enable `ToPrimitive`, `FromPrimitive`, `ToBytes`, `FromBytes`, `Num`, `Float`,
42+
`FloatCore` and `Bounded` trait implementations from the
43+
[`num-traits`](https://crates.io/crates/num-traits) crate.
4344

4445
- **`bytemuck`** — Enable `Zeroable` and `Pod` trait implementations from the
4546
[`bytemuck`](https://crates.io/crates/bytemuck) crate.

src/bfloat.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ mod test {
13321332
#[allow(unused_imports)]
13331333
use core::cmp::Ordering;
13341334
#[cfg(feature = "num-traits")]
1335-
use num_traits::{AsPrimitive, FromPrimitive, ToPrimitive};
1335+
use num_traits::{AsPrimitive, FromBytes, FromPrimitive, ToBytes, ToPrimitive};
13361336
use quickcheck_macros::quickcheck;
13371337

13381338
#[cfg(feature = "num-traits")]
@@ -1367,6 +1367,16 @@ mod test {
13671367
assert_eq!(<bf16 as FromPrimitive>::from_f64(2.0).unwrap(), two);
13681368
}
13691369

1370+
#[cfg(feature = "num-traits")]
1371+
#[test]
1372+
fn to_and_from_bytes() {
1373+
let two = bf16::from_f32(2.0);
1374+
assert_eq!(<bf16 as ToBytes>::to_le_bytes(&two), [0, 64]);
1375+
assert_eq!(<bf16 as FromBytes>::from_le_bytes(&[0, 64]), two);
1376+
assert_eq!(<bf16 as ToBytes>::to_be_bytes(&two), [64, 0]);
1377+
assert_eq!(<bf16 as FromBytes>::from_be_bytes(&[64, 0]), two);
1378+
}
1379+
13701380
#[test]
13711381
fn test_bf16_consts_from_f32() {
13721382
let one = bf16::from_f32(1.0);

src/binary16.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ mod test {
13491349
#[allow(unused_imports)]
13501350
use core::cmp::Ordering;
13511351
#[cfg(feature = "num-traits")]
1352-
use num_traits::{AsPrimitive, FromPrimitive, ToPrimitive};
1352+
use num_traits::{AsPrimitive, FromBytes, FromPrimitive, ToBytes, ToPrimitive};
13531353
use quickcheck_macros::quickcheck;
13541354

13551355
#[cfg(feature = "num-traits")]
@@ -1384,6 +1384,16 @@ mod test {
13841384
assert_eq!(<f16 as FromPrimitive>::from_f64(2.0).unwrap(), two);
13851385
}
13861386

1387+
#[cfg(feature = "num-traits")]
1388+
#[test]
1389+
fn to_and_from_bytes() {
1390+
let two = f16::from_f32(2.0);
1391+
assert_eq!(<f16 as ToBytes>::to_le_bytes(&two), [0, 64]);
1392+
assert_eq!(<f16 as FromBytes>::from_le_bytes(&[0, 64]), two);
1393+
assert_eq!(<f16 as ToBytes>::to_be_bytes(&two), [64, 0]);
1394+
assert_eq!(<f16 as FromBytes>::from_be_bytes(&[64, 0]), two);
1395+
}
1396+
13871397
#[test]
13881398
fn test_f16_consts() {
13891399
// DIGITS

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@
8888
//! [`Deserialize`] traits for both [`f16`] and [`bf16`].
8989
//!
9090
//! - **`num-traits`** — Adds support for the [`num-traits`] crate by implementing [`ToPrimitive`],
91-
//! [`FromPrimitive`], [`AsPrimitive`], [`Num`], [`Float`], [`FloatCore`], and [`Bounded`] traits
92-
//! for both [`f16`] and [`bf16`].
91+
//! [`FromPrimitive`], [`ToBytes`], [`FromBytes`], [`AsPrimitive`], [`Num`], [`Float`],
92+
//! [`FloatCore`], and [`Bounded`] traits for both [`f16`] and [`bf16`].
9393
//!
9494
//! - **`bytemuck`** — Adds support for the [`bytemuck`] crate by implementing [`Zeroable`] and
9595
//! [`Pod`] traits for both [`f16`] and [`bf16`].
@@ -155,6 +155,8 @@
155155
doc = "
156156
[`ToPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.ToPrimitive.html
157157
[`FromPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.FromPrimitive.html
158+
[`ToBytes`]: https://docs.rs/num-traits/*/num_traits/ops/bytes/trait.ToBytes.html
159+
[`FromBytes`]: https://docs.rs/num-traits/*/num_traits/ops/bytes/trait.FromBytes.html
158160
[`AsPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.AsPrimitive.html
159161
[`Num`]: https://docs.rs/num-traits/*/num_traits/trait.Num.html
160162
[`Float`]: https://docs.rs/num-traits/*/num_traits/float/trait.Float.html

src/num_traits.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::{bf16, f16};
22
use core::cmp::Ordering;
33
use core::{num::FpCategory, ops::Div};
44
use num_traits::{
5-
AsPrimitive, Bounded, FloatConst, FromPrimitive, Num, NumCast, One, ToPrimitive, Zero,
5+
AsPrimitive, Bounded, FloatConst, FromBytes, FromPrimitive, Num, NumCast, One, ToBytes,
6+
ToPrimitive, Zero,
67
};
78

89
impl ToPrimitive for f16 {
@@ -744,6 +745,38 @@ impl_as_primitive_f16_from!(usize, from_f32);
744745
impl_as_primitive_f16_from!(f32, from_f32);
745746
impl_as_primitive_f16_from!(f64, from_f64);
746747

748+
impl ToBytes for f16 {
749+
type Bytes = [u8; 2];
750+
751+
fn to_be_bytes(&self) -> Self::Bytes {
752+
Self::to_be_bytes(*self)
753+
}
754+
755+
fn to_le_bytes(&self) -> Self::Bytes {
756+
Self::to_le_bytes(*self)
757+
}
758+
759+
fn to_ne_bytes(&self) -> Self::Bytes {
760+
Self::to_ne_bytes(*self)
761+
}
762+
}
763+
764+
impl FromBytes for f16 {
765+
type Bytes = [u8; 2];
766+
767+
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
768+
Self::from_be_bytes(*bytes)
769+
}
770+
771+
fn from_le_bytes(bytes: &Self::Bytes) -> Self {
772+
Self::from_le_bytes(*bytes)
773+
}
774+
775+
fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
776+
Self::from_ne_bytes(*bytes)
777+
}
778+
}
779+
747780
impl ToPrimitive for bf16 {
748781
#[inline]
749782
fn to_i64(&self) -> Option<i64> {
@@ -1483,3 +1516,35 @@ impl_as_primitive_bf16_from!(isize, from_f32);
14831516
impl_as_primitive_bf16_from!(usize, from_f32);
14841517
impl_as_primitive_bf16_from!(f32, from_f32);
14851518
impl_as_primitive_bf16_from!(f64, from_f64);
1519+
1520+
impl ToBytes for bf16 {
1521+
type Bytes = [u8; 2];
1522+
1523+
fn to_be_bytes(&self) -> Self::Bytes {
1524+
Self::to_be_bytes(*self)
1525+
}
1526+
1527+
fn to_le_bytes(&self) -> Self::Bytes {
1528+
Self::to_le_bytes(*self)
1529+
}
1530+
1531+
fn to_ne_bytes(&self) -> Self::Bytes {
1532+
Self::to_ne_bytes(*self)
1533+
}
1534+
}
1535+
1536+
impl FromBytes for bf16 {
1537+
type Bytes = [u8; 2];
1538+
1539+
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
1540+
Self::from_be_bytes(*bytes)
1541+
}
1542+
1543+
fn from_le_bytes(bytes: &Self::Bytes) -> Self {
1544+
Self::from_le_bytes(*bytes)
1545+
}
1546+
1547+
fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
1548+
Self::from_ne_bytes(*bytes)
1549+
}
1550+
}

0 commit comments

Comments
 (0)