Skip to content

Commit

Permalink
Add le/be float read/write methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Nov 17, 2024
1 parent b3c34ae commit 5a8962b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to `parsenic` will be documented in this file.
The format is based on [Keep a Changelog], and this project adheres to
[Semantic Versioning].

## [0.2.1] - 2024-11-16

### Added

- `le::Read::f32()`
- `le::Read::f64()`
- `le::Write::f32()`
- `le::Write::f64()`
- `be::Read::f32()`
- `be::Read::f64()`
- `be::Write::f32()`
- `be::Write::f64()`

## [0.2.0] - 2024-11-05

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parsenic"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
license = "Apache-2.0 OR BSL-1.0 OR MIT"
description = "A simple no-std/no-alloc I/O and parsing crate"
Expand Down
10 changes: 10 additions & 0 deletions src/be/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ pub trait Read: crate::Read {
fn i128(&mut self) -> LenResult<i128> {
Ok(i128::from_be_bytes(self.array()?))
}

/// Read the next big endian `f32`
fn f32(&mut self) -> LenResult<f32> {
Ok(f32::from_be_bytes(self.array()?))
}

/// Read the next big endian `f64`
fn f64(&mut self) -> LenResult<f64> {
Ok(f64::from_be_bytes(self.array()?))
}
}
10 changes: 10 additions & 0 deletions src/be/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ pub trait Write: crate::Write {
fn i128(&mut self, int: i128) -> FullResult {
self.bytes(int.to_be_bytes())
}

/// Write out a big endian encoded 32-bit float.
fn f32(&mut self, float: f32) -> FullResult {
self.bytes(float.to_be_bytes())
}

/// Write out a big endian encoded 64-bit float.
fn f64(&mut self, float: f64) -> FullResult {
self.bytes(float.to_be_bytes())
}
}
10 changes: 10 additions & 0 deletions src/le/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ pub trait Read: crate::Read {
fn i128(&mut self) -> LenResult<i128> {
Ok(i128::from_le_bytes(self.array()?))
}

/// Read the next little endian `f32`
fn f32(&mut self) -> LenResult<f32> {
Ok(f32::from_le_bytes(self.array()?))
}

/// Read the next little endian `f64`
fn f64(&mut self) -> LenResult<f64> {
Ok(f64::from_le_bytes(self.array()?))
}
}
10 changes: 10 additions & 0 deletions src/le/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ pub trait Write: crate::Write {
fn i128(&mut self, int: i128) -> FullResult {
self.bytes(int.to_le_bytes())
}

/// Write out a little endian encoded 32-bit float.
fn f32(&mut self, float: f32) -> FullResult {
self.bytes(float.to_le_bytes())
}

/// Write out a little endian encoded 64-bit float.
fn f64(&mut self, float: f64) -> FullResult {
self.bytes(float.to_le_bytes())
}
}

0 comments on commit 5a8962b

Please sign in to comment.