-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve documentation to prepare for 0.3 release #315
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,67 @@ | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
//! As its name suggests, the [VersionSet] trait describes sets of versions. | ||
//! | ||
//! One needs to define | ||
//! - the associate type for versions, | ||
//! - two constructors for the empty set and a singleton set, | ||
//! - the complement and intersection set operations, | ||
//! - and a function to evaluate membership of versions. | ||
//! | ||
//! Two functions are automatically derived, thanks to the mathematical properties of sets. | ||
//! You can overwrite those implementations, but we highly recommend that you don't, | ||
//! except if you are confident in a correct implementation that brings much performance gains. | ||
//! | ||
//! It is also extremely important that the `Eq` trait is correctly implemented. | ||
//! In particular, you can only use `#[derive(Eq, PartialEq)]` if `Eq` is strictly equivalent to the | ||
//! structural equality, i.e. if version sets have canonical representations. | ||
//! Such problems may arise if your implementations of `complement()` and `intersection()` do not | ||
//! return canonical representations so be careful there. | ||
|
||
use std::fmt::{Debug, Display}; | ||
|
||
use crate::Ranges; | ||
|
||
/// Trait describing sets of versions. | ||
/// A set of versions. | ||
/// | ||
/// See [`Ranges`] for an implementation. | ||
/// | ||
/// The methods with default implementations can be overwritten for better performance, but their | ||
/// output must be equal to the default implementation. | ||
/// | ||
/// # Equality | ||
/// | ||
/// It is important that the `Eq` trait is implemented so that if two sets contain the same | ||
/// versions, they are equal under `Eq`. In particular, you can only use `#[derive(Eq, PartialEq)]` | ||
/// if `Eq` is strictly equivalent to the structural equality, i.e. if version sets are always | ||
/// stored in a canonical representations. Such problems may arise if your implementations of | ||
/// `complement()` and `intersection()` do not return canonical representations. | ||
/// | ||
/// For example, `>=1,<4 || >=2,<5` and `>=1,<4 || >=3,<5` are equal, because they can both be | ||
/// normalized to `>=1,<5`. | ||
/// | ||
/// Note that pubgrub does not know which versions actually exist for a package, the contract | ||
/// is about upholding the mathematical properties of set operations, assuming all versions are | ||
/// possible. This is required for the solver to determine the relationship of version sets to each | ||
/// other. | ||
pub trait VersionSet: Debug + Display + Clone + Eq { | ||
/// Version type associated with the sets manipulated. | ||
type V: Debug + Display + Clone + Ord; | ||
|
||
// Constructors | ||
/// Constructor for an empty set containing no version. | ||
|
||
/// An empty set containing no version. | ||
fn empty() -> Self; | ||
/// Constructor for a set containing exactly one version. | ||
|
||
/// A set containing only the given version. | ||
fn singleton(v: Self::V) -> Self; | ||
|
||
// Operations | ||
/// Compute the complement of this set. | ||
|
||
/// The set of all version that are not in this set. | ||
fn complement(&self) -> Self; | ||
/// Compute the intersection with another set. | ||
|
||
/// The set of all versions that are in both sets. | ||
fn intersection(&self, other: &Self) -> Self; | ||
|
||
// Membership | ||
/// Evaluate membership of a version in this set. | ||
/// Whether the version is part of this set. | ||
fn contains(&self, v: &Self::V) -> bool; | ||
|
||
// Automatically implemented functions ########################### | ||
// Automatically implemented functions | ||
|
||
/// Constructor for the set containing all versions. | ||
/// Automatically implemented as `Self::empty().complement()`. | ||
/// The set containing all versions. | ||
/// | ||
/// The default implementation is the complement of the empty set. | ||
fn full() -> Self { | ||
Self::empty().complement() | ||
} | ||
|
||
/// Compute the union with another set. | ||
/// Thanks to set properties, this is automatically implemented as: | ||
/// `self.complement().intersection(&other.complement()).complement()` | ||
/// The set of all versions that are either (or both) of the sets. | ||
/// | ||
/// The default implementation is complement of the intersection of the complements of both sets | ||
/// (De Morgan's law). | ||
fn union(&self, other: &Self) -> Self { | ||
self.complement() | ||
.intersection(&other.complement()) | ||
|
@@ -71,6 +79,7 @@ pub trait VersionSet: Debug + Display + Clone + Eq { | |
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small grammar things on these two.
and
|
||
} | ||
|
||
/// [`Ranges`] contains optimized implementations of all operations. | ||
impl<T: Debug + Display + Clone + Eq + Ord> VersionSet for Ranges<T> { | ||
type V = T; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps explicitly call out that this example is for integers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about them in the context of PEP 440 and semver, are there cases when this doesn't hold?