Skip to content

Commit 187175a

Browse files
authored
Add support for serde to the Primes struct, hidden behind a feature. (#41)
* impl serde::serialize when the serde feature is on * Add serde support for deserialization with serde_arrays * Document the serde feature in lib.rs, README.md and Cargo.toml * Document serde feature in changelog
1 parent c49facc commit 187175a

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
This file contains the changes to the crate since version 0.4.8.
22

3+
# 0.8.2
4+
5+
- Add the `serde` feature that derives the `Serialize` and `Deserialize` traits from `serde` for the `Primes` struct.
6+
37
# 0.8.1
48

59
- Added a crate feature flag badge to the docs.
610
- Mention what can be done with the crate clearer in the description.
711

8-
912
# 0.8.0
1013

1114
## Breaking changes

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ categories = ["mathematics", "no-std", "no-std::no-alloc", "algorithms"]
99
description = "Work with prime numbers in const contexts. Prime generation, primality testing, prime counting, and more."
1010
repository = "https://github.com/JSorngard/const-primes/"
1111

12+
[dependencies]
13+
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true}
14+
serde_arrays = {version = "0.1.0", optional = true}
15+
1216
[dev-dependencies]
1317
criterion = { version = "0.5", features = ["html_reports"] }
1418
rand = "0.8"
1519

1620
[features]
1721
# Implements the `Error` trait from the standard library for the error types.
1822
std = []
23+
# Derives the `Serialize` and `Deserialize` traits from [`serde`](https://crates.io/crates/serde) for the `Primes` struct.
24+
serde = ["dep:serde", "dep:serde_arrays"]
1925

2026
# docs.rs-specific configuration. Taken from <https://stackoverflow.com/a/61417700/>.
2127
[package.metadata.docs.rs]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ and more!
126126

127127
## Features
128128

129-
`std`: implements the `Error` trait from the standard library for the error types.
129+
`std`: implements the `Error` trait from the standard library for the error types.
130+
`serde`: derives the `Serialize` and `Deserialize` traits from [`serde`](https://crates.io/crates/serde) for the `Primes` struct.
130131

131132
## License
132133

src/cache/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ use crate::{primes, Underlying};
4141
/// assert_eq!(CACHE.count_primes_leq(1000), None);
4242
/// ```
4343
#[derive(Debug, Clone, Copy, Eq, Ord, Hash)]
44-
pub struct Primes<const N: usize>([Underlying; N]);
44+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
45+
pub struct Primes<const N: usize>(
46+
#[cfg_attr(feature = "serde", serde(with = "serde_arrays"))] [Underlying; N],
47+
);
4548

4649
impl<const N: usize> Primes<N> {
4750
/// Generates a new instance that contains the first `N` primes.

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@
121121
//!
122122
//! # Features
123123
//!
124-
//! `std`: implements the `Error` trait from the standard library for the error types.
124+
//! `std`: implements the [`Error`](std::error::Error) trait from the standard library for the error types.
125+
//! `serde`: derives the [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize) traits from [`serde`](https://docs.rs/serde/latest/serde/) for the [`Primes`] struct.
125126
126127
#![forbid(unsafe_code)]
127128
#![cfg_attr(not(feature = "std"), no_std)]

0 commit comments

Comments
 (0)