Skip to content

Commit

Permalink
make serde optional using a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Feb 11, 2024
1 parent 1fbd06a commit 7e1babc
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 233 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Renamed `InclusiveInterval::contains()` to
`InclusiveInterval::contains_point()` to match the new
`InclusiveInterval::contains_interval()` method
- `serde`'s Serialize and Deserialize implementations are now optional via a
"serde" feature which is documented in the features section of the
readme/top-level module docs

## 0.8.0 - 2024-01-28

Expand Down
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ keywords = ["data-structures", "map", "data", "library"]
categories = ["data-structures"]

[dependencies]
serde = { version = "1.0.193", features = ["derive"], default-features = false }
serde = { version = "1.0.193", features = [
"derive",
], default-features = false, optional = true }
btree_monstrousity = { version = "0.0.4", features = [
"btree_drain_filter",
"btree_cursors",
], default-features = false }
itertools = { version = "0.12.0", default-features = false }
smallvec = { version = "1.13.1", default-features = false }

[features]
default = []
serde = ["dep:serde"]

[dev-dependencies]
pretty_assertions = "1.4.0"

Expand Down
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ from an empty map if the type was an infinite type such as `BigInt`
since it has no maximum value.

A handy trick you can use to pretend to have infinite types when you
don't expect to reach to top end of your type is to use [`Actual
Infinity`] to pretend you have an `Infinity`. For example, if you were
don't expect to reach to top end of your type is to use [`Actual Infinity`] to pretend you have an `Infinity`. For example, if you were
using `u8` as your point type then you could create a wrapper type such
as this:

Expand Down Expand Up @@ -291,7 +290,10 @@ See Wikipedia's article on mathematical Intervals:

## Features

This crate currently has no features
|Feature Name| Description|
|-----------|-----|
|`default`|The implicit default feature enabled by default which currently does not activate any other features|
|`serde`|Enables the optional `serde` dependency and implements `serde::Serialize` and `serde::Deserialize` on all the types in this crate|

## Credit

Expand Down Expand Up @@ -354,26 +356,26 @@ topic area, beware my biases when reading:
- <https://docs.rs/bio> and <https://docs.rs/rudac>
Both essentially identical to `store-interval-tree` as it looks like
`store-interval-tree` is a fork of `rudac`'s interval tree. `bio` in
particular seems targeted at bioinfographics.
particular seems targeted at bio-infographics.

[`actual infinity`]: https://en.wikipedia.org/wiki/Actual_infinity
[`bigint`]: https://docs.rs/num-bigint/latest/num_bigint/struct.BigInt.html
[`btreemap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
[`btree_monstrousity`]: https://github.com/ripytide/btree_monstrousity
[`range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
[`rangemap`]: https://docs.rs/rangemap/latest/rangemap/
[`rangeinclusive`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
[`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics
[`copse`]: https://github.com/eggyal/copse
[`discrete_range_map`]: https://docs.rs/discrete_range_map
[`discrete`]: https://en.wikipedia.org/wiki/Discrete_mathematics
[`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics
[`interval-mathematics`]: https://en.wikipedia.org/wiki/Interval_(mathematics)
[`actual infinity`]: https://en.wikipedia.org/wiki/Actual_infinity
[`finite`]: https://en.wiktionary.org/wiki/finite#Adjective
[`range_bounds_map`]: https://docs.rs/range_bounds_map
[`discrete_range_map`]: https://docs.rs/discrete_range_map
[`get_key_value_at_point()`]: https://docs.rs/nodit/latest/nodit/nodit/map/struct.NoditMap.html#method.get_key_value_at_point
[`gqdit`]: https://docs.rs/nodit/latest/nodit/gqdit/struct.Gqdit.html
[`interval-mathematics`]: https://en.wikipedia.org/wiki/Interval_(mathematics)
[`noditmap`]: https://docs.rs/nodit/latest/nodit/nodit/map/struct.NoditMap.html
[`noditset`]: https://docs.rs/nodit/latest/nodit/nodit/set/struct.NoditSet.html
[`nodit`]: https://docs.rs/nodit
[`bigint`]: https://docs.rs/num-bigint/latest/num_bigint/struct.BigInt.html
[`num_bigint`]: https://docs.rs/num-bigint
[`get_key_value_at_point()`]: https://docs.rs/nodit/latest/nodit/nodit/map/struct.NoditMap.html#method.get_key_value_at_point
[`NoditMap`]: https://docs.rs/nodit/latest/nodit/nodit/map/struct.NoditMap.html
[`NoditSet`]: https://docs.rs/nodit/latest/nodit/nodit/set/struct.NoditSet.html
[`ZosditMap`]: https://docs.rs/nodit/latest/nodit/zosdit/map/struct.ZosditMap.html
[`Gqdit`]: https://docs.rs/nodit/latest/nodit/gqdit/struct.Gqdit.html
[`rangeinclusive`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
[`rangemap`]: https://docs.rs/rangemap/latest/rangemap/
[`range_bounds_map`]: https://docs.rs/range_bounds_map
[`range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
[`zosditmap`]: https://docs.rs/nodit/latest/nodit/zosdit/map/struct.ZosditMap.html
55 changes: 32 additions & 23 deletions src/gqdit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use alloc::collections::BTreeSet;
use alloc::vec::Vec;

use itertools::Itertools;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::interval::{ii, iu, ui, uu};
use crate::utils::invalid_interval_panic;
Expand Down Expand Up @@ -190,7 +189,8 @@ where
//we don't want end ones as they are
//handled separately
let non_end_gaps = valid_gaps.filter(|gap| {
!gap.contains_point(interval.start()) && !gap.contains_point(interval.end())
!gap.contains_point(interval.start())
&& !gap.contains_point(interval.end())
});

//instead of using possibly-partial end gaps we will
Expand Down Expand Up @@ -558,32 +558,41 @@ where
}
}

impl<I, K, D> Serialize for Gqdit<I, K, D>
where
I: PointType,
K: IntervalType<I> + Serialize,
D: IdType,
BTreeSet<D>: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
#[cfg(feature = "serde")]
mod serde {
use alloc::collections::BTreeSet;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::{Gqdit, IdType, IntervalType, NoditMap, PointType};

impl<I, K, D> Serialize for Gqdit<I, K, D>
where
S: Serializer,
I: PointType,
K: IntervalType<I> + Serialize,
D: IdType,
BTreeSet<D>: Serialize,
{
self.inner.serialize(serializer)
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.inner.serialize(serializer)
}
}
}

impl<'de, I, K, D> Deserialize<'de> for Gqdit<I, K, D>
where
I: PointType,
K: IntervalType<I> + Deserialize<'de>,
D: IdType,
BTreeSet<D>: Deserialize<'de>,
{
fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>
impl<'de, I, K, D> Deserialize<'de> for Gqdit<I, K, D>
where
De: Deserializer<'de>,
I: PointType,
K: IntervalType<I> + Deserialize<'de>,
D: IdType,
BTreeSet<D>: Deserialize<'de>,
{
NoditMap::deserialize(deserializer).map(|x| Gqdit { inner: x })
fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>
where
De: Deserializer<'de>,
{
NoditMap::deserialize(deserializer).map(|x| Gqdit { inner: x })
}
}
}
7 changes: 3 additions & 4 deletions src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use core::ops::{Bound, Range, RangeBounds, RangeInclusive};

use serde::{Deserialize, Serialize};

use crate::utils::{invalid_interval_panic, sorted_config, SortedConfig};
use crate::{IntervalType, PointType};

Expand Down Expand Up @@ -39,7 +37,8 @@ use crate::{IntervalType, PointType};
///
/// let invalid_interval = ee(4, 4);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Interval<I> {
/// The start of the interval, inclusive.
pub(crate) start: I,
Expand Down Expand Up @@ -499,7 +498,7 @@ pub trait InclusiveInterval<I>: Copy + From<Interval<I>> {
fn intersection<Q>(&self, other: &Q) -> Option<Self>
where
I: PointType,
Q: IntervalType<I>,
Q: IntervalType<I>,
Self: From<Interval<I>>,
{
let intersect_start = I::max(self.start(), other.start());
Expand Down
51 changes: 26 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@
//! traits on the value type are sometimes `Clone` or `Eq` but only for some
//! methods so if in doubt check a methods trait bounds.
//!
//! ## `NoditMap` Example using an Inclusive-Inclusive interval
//! ## Example using an Inclusive-Exclusive interval
//!
//! ```rust
//! use nodit::interval::ii;
//! use nodit::interval::ie;
//! use nodit::NoditMap;
//!
//! let mut map = NoditMap::new();
//!
//! map.insert_strict(ii(0, 4), true);
//! map.insert_strict(ii(5, 10), false);
//! map.insert_strict(ie(0, 5), true);
//! map.insert_strict(ie(5, 10), false);
//!
//! assert_eq!(map.overlaps(ii(-2, 12)), true);
//! assert_eq!(map.overlaps(ie(-2, 12)), true);
//! assert_eq!(map.contains_point(20), false);
//! assert_eq!(map.contains_point(5), true);
//! assert_eq!(map.get_key_value_at_point(2), Ok((&ii(0, 4), &true)));
//! ```
//!
//! ## `NoditMap` Example using a custom interval type
//! ## Example using a custom interval type
//!
//! ```rust
//! use std::ops::{Bound, RangeBounds};
Expand Down Expand Up @@ -139,8 +138,7 @@
//! since it has no maximum value.
//!
//! A handy trick you can use to pretend to have infinite types when you
//! don't expect to reach to top end of your type is to use [`Actual
//! Infinity`] to pretend you have an `Infinity`. For example, if you were
//! don't expect to reach to top end of your type is to use [`Actual Infinity`] to pretend you have an `Infinity`. For example, if you were
//! using `u8` as your point type then you could create a wrapper type such
//! as this:
//!
Expand Down Expand Up @@ -281,7 +279,10 @@
//!
//! ## Features
//!
//! This crate currently has no features
//! |Feature Name| Description|
//! |-----------|-----|
//! |`default`|The implicit default feature enabled by default which currently does not activate any other features|
//! |`serde`|Enables the optional `serde` dependency and implements `serde::Serialize` and `serde::Deserialize` on all the types in this crate|
//!
//! ## Credit
//!
Expand Down Expand Up @@ -344,29 +345,29 @@
//! - <https://docs.rs/bio> and <https://docs.rs/rudac>
//! Both essentially identical to `store-interval-tree` as it looks like
//! `store-interval-tree` is a fork of `rudac`'s interval tree. `bio` in
//! particular seems targeted at bioinfographics.
//! particular seems targeted at bio-infographics.
//!
//! [`actual infinity`]: https://en.wikipedia.org/wiki/Actual_infinity
//! [`bigint`]: https://docs.rs/num-bigint/latest/num_bigint/struct.BigInt.html
//! [`btreemap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
//! [`btree_monstrousity`]: https://github.com/ripytide/btree_monstrousity
//! [`range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
//! [`rangemap`]: https://docs.rs/rangemap/latest/rangemap/
//! [`rangeinclusive`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
//! [`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics
//! [`copse`]: https://github.com/eggyal/copse
//! [`discrete_range_map`]: https://docs.rs/discrete_range_map
//! [`discrete`]: https://en.wikipedia.org/wiki/Discrete_mathematics
//! [`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics
//! [`interval-mathematics`]: https://en.wikipedia.org/wiki/Interval_(mathematics)
//! [`actual infinity`]: https://en.wikipedia.org/wiki/Actual_infinity
//! [`finite`]: https://en.wiktionary.org/wiki/finite#Adjective
//! [`range_bounds_map`]: https://docs.rs/range_bounds_map
//! [`discrete_range_map`]: https://docs.rs/discrete_range_map
//! [`get_key_value_at_point()`]: https://docs.rs/nodit/latest/nodit/nodit/map/struct.NoditMap.html#method.get_key_value_at_point
//! [`gqdit`]: https://docs.rs/nodit/latest/nodit/gqdit/struct.Gqdit.html
//! [`interval-mathematics`]: https://en.wikipedia.org/wiki/Interval_(mathematics)
//! [`noditmap`]: https://docs.rs/nodit/latest/nodit/nodit/map/struct.NoditMap.html
//! [`noditset`]: https://docs.rs/nodit/latest/nodit/nodit/set/struct.NoditSet.html
//! [`nodit`]: https://docs.rs/nodit
//! [`bigint`]: https://docs.rs/num-bigint/latest/num_bigint/struct.BigInt.html
//! [`num_bigint`]: https://docs.rs/num-bigint
//! [`get_key_value_at_point()`]: https://docs.rs/nodit/latest/nodit/nodit/map/struct.NoditMap.html#method.get_key_value_at_point
//! [`NoditMap`]: https://docs.rs/nodit/latest/nodit/nodit/map/struct.NoditMap.html
//! [`NoditSet`]: https://docs.rs/nodit/latest/nodit/nodit/set/struct.NoditSet.html
//! [`ZosditMap`]: https://docs.rs/nodit/latest/nodit/zosdit/map/struct.ZosditMap.html
//! [`Gqdit`]: https://docs.rs/nodit/latest/nodit/gqdit/struct.Gqdit.html
//! [`rangeinclusive`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
//! [`rangemap`]: https://docs.rs/rangemap/latest/rangemap/
//! [`range_bounds_map`]: https://docs.rs/range_bounds_map
//! [`range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
//! [`zosditmap`]: https://docs.rs/nodit/latest/nodit/zosdit/map/struct.ZosditMap.html
#![allow(clippy::tabs_in_doc_comments)]
#![allow(clippy::needless_return)]
Expand Down
Loading

0 comments on commit 7e1babc

Please sign in to comment.