Skip to content

Commit

Permalink
Document invariant on Ranges (pubgrub-rs#277)
Browse files Browse the repository at this point in the history
* Document invariant on Ranges

* More explanantion
  • Loading branch information
konstin authored Nov 11, 2024
1 parent 1f3baca commit 36c2e41
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions version-ranges/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ use proptest::prelude::*;
use smallvec::{smallvec, SmallVec};

/// Ranges represents multiple intervals of a continuous range of monotone increasing values.
///
/// Internally, [`Ranges`] are an ordered list of segments, where segment is a bounds pair.
///
/// Invariants:
/// 1. The segments are sorted, from lowest to highest (through `Ord`).
/// 2. Each segment contains at least one version (start < end).
/// 3. There is at least one version between two segments.
///
/// These ensure that equivalent instances have an identical representation, which is important
/// for `Eq` and `Hash`. Note that this representation cannot strictly guaranty equality of
/// [`Ranges`] with equality of its representation without also knowing the nature of the underlying
/// versions. In particular, if the version space is discrete, different representations, using
/// different types of bounds (exclusive/inclusive) may correspond to the same set of existing
/// versions. It is a tradeoff we acknowledge, but which makes representations of continuous version
/// sets more accessible, to better handle features like pre-releases and other types of version
/// modifiers. For example, `[(Included(3u32), Excluded(7u32))]` and
/// `[(Included(3u32), Included(6u32))]` refer to the same version set, since there is no version
/// between 6 and 7, which this crate doesn't know about.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
Expand Down Expand Up @@ -283,6 +302,7 @@ impl<V: Ord> Ranges<V> {
}
}

/// See [`Ranges`] for the invariants checked.
fn check_invariants(self) -> Self {
if cfg!(debug_assertions) {
for p in self.segments.as_slice().windows(2) {
Expand Down

0 comments on commit 36c2e41

Please sign in to comment.