Skip to content
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

subscriber: make PartialOrd & Ord impls more correct #995

Merged
merged 2 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions tracing-subscriber/src/filter/env/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ impl Default for Directive {

impl PartialOrd for Directive {
fn partial_cmp(&self, other: &Directive) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Directive {
fn cmp(&self, other: &Directive) -> Ordering {
// We attempt to order directives by how "specific" they are. This
// ensures that we try the most specific directives first when
// attempting to match a piece of metadata.
Expand Down Expand Up @@ -321,14 +327,7 @@ impl PartialOrd for Directive {
}
}

Some(ordering)
}
}

impl Ord for Directive {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other)
.expect("Directive::partial_cmp should define a total order")
ordering
}
}

Expand Down Expand Up @@ -502,8 +501,8 @@ impl Statics {
}
}

impl PartialOrd for StaticDirective {
fn partial_cmp(&self, other: &StaticDirective) -> Option<Ordering> {
impl Ord for StaticDirective {
fn cmp(&self, other: &StaticDirective) -> Ordering {
// We attempt to order directives by how "specific" they are. This
// ensures that we try the most specific directives first when
// attempting to match a piece of metadata.
Expand Down Expand Up @@ -542,14 +541,13 @@ impl PartialOrd for StaticDirective {
}
}

Some(ordering)
ordering
}
}

impl Ord for StaticDirective {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other)
.expect("StaticDirective::partial_cmp should define a total order")
impl PartialOrd for StaticDirective {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

Expand Down
20 changes: 12 additions & 8 deletions tracing-subscriber/src/filter/env/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
use super::{FieldMap, LevelFilter};
use tracing_core::field::{Field, Visit};

#[derive(Debug, Eq, PartialEq, Ord)]
#[derive(Debug, Eq, PartialEq)]
pub(crate) struct Match {
pub(crate) name: String, // TODO: allow match patterns for names?
pub(crate) value: Option<ValueMatch>,
Expand Down Expand Up @@ -95,8 +95,8 @@ impl fmt::Display for Match {
}
}

impl PartialOrd for Match {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
impl Ord for Match {
fn cmp(&self, other: &Self) -> Ordering {
// Ordering for `Match` directives is based first on _whether_ a value
// is matched or not. This is semantically meaningful --- we would
// prefer to check directives that match values first as they are more
Expand All @@ -113,11 +113,15 @@ impl PartialOrd for Match {
// This ordering is no longer semantically meaningful but is necessary
// so that the directives can be stored in the `BTreeMap` in a defined
// order.
Some(
has_value
.then_with(|| self.name.cmp(&other.name))
.then_with(|| self.value.cmp(&other.value)),
)
has_value
.then_with(|| self.name.cmp(&other.name))
.then_with(|| self.value.cmp(&other.value))
}
}

impl PartialOrd for Match {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

Expand Down