Skip to content

Commit

Permalink
Add support for serde to the Primes struct, hidden behind a featu…
Browse files Browse the repository at this point in the history
…re. (#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
  • Loading branch information
JSorngard authored Jun 7, 2024
1 parent c49facc commit 187175a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
This file contains the changes to the crate since version 0.4.8.

# 0.8.2

- Add the `serde` feature that derives the `Serialize` and `Deserialize` traits from `serde` for the `Primes` struct.

# 0.8.1

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


# 0.8.0

## Breaking changes
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ categories = ["mathematics", "no-std", "no-std::no-alloc", "algorithms"]
description = "Work with prime numbers in const contexts. Prime generation, primality testing, prime counting, and more."
repository = "https://github.com/JSorngard/const-primes/"

[dependencies]
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true}
serde_arrays = {version = "0.1.0", optional = true}

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
rand = "0.8"

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

# docs.rs-specific configuration. Taken from <https://stackoverflow.com/a/61417700/>.
[package.metadata.docs.rs]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ and more!

## Features

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

## License

Expand Down
5 changes: 4 additions & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ use crate::{primes, Underlying};
/// assert_eq!(CACHE.count_primes_leq(1000), None);
/// ```
#[derive(Debug, Clone, Copy, Eq, Ord, Hash)]
pub struct Primes<const N: usize>([Underlying; N]);
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Primes<const N: usize>(
#[cfg_attr(feature = "serde", serde(with = "serde_arrays"))] [Underlying; N],
);

impl<const N: usize> Primes<N> {
/// Generates a new instance that contains the first `N` primes.
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
//!
//! # Features
//!
//! `std`: implements the `Error` trait from the standard library for the error types.
//! `std`: implements the [`Error`](std::error::Error) trait from the standard library for the error types.
//! `serde`: derives the [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize) traits from [`serde`](https://docs.rs/serde/latest/serde/) for the [`Primes`] struct.
#![forbid(unsafe_code)]
#![cfg_attr(not(feature = "std"), no_std)]
Expand Down

0 comments on commit 187175a

Please sign in to comment.