Skip to content

Commit

Permalink
chore: reverse the polarity of conversions, fix clippy (#1335)
Browse files Browse the repository at this point in the history
This backports PR #1335 from `master` to `v0.1.x`.

Clippy now warns about implementing `Into` rather than `From`, since
`From` automatically provides `Into` but `Into` does not provide `From`.

This commit fixes the direction of those conversions, placating Clippy.

Additionally, it fixes a redundant use of slice syntax that Clippy also
complained about.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Mar 29, 2021
1 parent 8868541 commit 84ff3a4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl FieldSet {
{
ValueSet {
fields: self,
values: &values.borrow()[..],
values: values.borrow(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ impl From<Option<Level>> for LevelFilter {
}
}

impl Into<Option<Level>> for LevelFilter {
impl From<LevelFilter> for Option<Level> {
#[inline]
fn into(self) -> Option<Level> {
self.into_level()
fn from(filter: LevelFilter) -> Self {
filter.into_level()
}
}

Expand Down
33 changes: 18 additions & 15 deletions tracing-core/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ impl Id {
}
}

impl<'a> Into<Option<Id>> for &'a Id {
fn into(self) -> Option<Id> {
Some(self.clone())
impl<'a> From<&'a Id> for Option<Id> {
fn from(id: &'a Id) -> Self {
Some(id.clone())
}
}

Expand Down Expand Up @@ -293,26 +293,29 @@ impl Current {
}
}

impl<'a> Into<Option<&'a Id>> for &'a Current {
fn into(self) -> Option<&'a Id> {
self.id()
impl<'a> From<&'a Current> for Option<&'a Id> {
fn from(cur: &'a Current) -> Self {
cur.id()
}
}

impl<'a> Into<Option<Id>> for &'a Current {
fn into(self) -> Option<Id> {
self.id().cloned()
impl<'a> From<&'a Current> for Option<Id> {
fn from(cur: &'a Current) -> Self {
cur.id().cloned()
}
}

impl Into<Option<Id>> for Current {
fn into(self) -> Option<Id> {
self.id().cloned()
impl From<Current> for Option<Id> {
fn from(cur: Current) -> Self {
match cur.inner {
CurrentInner::Current { id, .. } => Some(id),
_ => None,
}
}
}

impl<'a> Into<Option<&'static Metadata<'static>>> for &'a Current {
fn into(self) -> Option<&'static Metadata<'static>> {
self.metadata()
impl<'a> From<&'a Current> for Option<&'static Metadata<'static>> {
fn from(cur: &'a Current) -> Self {
cur.metadata()
}
}
6 changes: 3 additions & 3 deletions tracing-subscriber/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,16 +572,16 @@ where
}
}

impl<N, E, F, W> Into<tracing_core::Dispatch> for SubscriberBuilder<N, E, F, W>
impl<N, E, F, W> From<SubscriberBuilder<N, E, F, W>> for tracing_core::Dispatch
where
N: for<'writer> FormatFields<'writer> + 'static,
E: FormatEvent<Registry, N> + 'static,
W: MakeWriter + 'static,
F: layer::Layer<Formatter<N, E, W>> + Send + Sync + 'static,
fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry> + Send + Sync + 'static,
{
fn into(self) -> tracing_core::Dispatch {
tracing_core::Dispatch::new(self.finish())
fn from(builder: SubscriberBuilder<N, E, F, W>) -> tracing_core::Dispatch {
tracing_core::Dispatch::new(builder.finish())
}
}

Expand Down
12 changes: 6 additions & 6 deletions tracing/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,15 +1300,15 @@ impl fmt::Debug for Span {
}
}

impl<'a> Into<Option<&'a Id>> for &'a Span {
fn into(self) -> Option<&'a Id> {
self.inner.as_ref().map(|inner| &inner.id)
impl<'a> From<&'a Span> for Option<&'a Id> {
fn from(span: &'a Span) -> Self {
span.inner.as_ref().map(|inner| &inner.id)
}
}

impl<'a> Into<Option<Id>> for &'a Span {
fn into(self) -> Option<Id> {
self.inner.as_ref().map(Inner::id)
impl<'a> From<&'a Span> for Option<Id> {
fn from(span: &'a Span) -> Self {
span.inner.as_ref().map(Inner::id)
}
}

Expand Down
6 changes: 3 additions & 3 deletions tracing/tests/support/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ impl MockField {
}
}

impl Into<Expect> for MockField {
fn into(self) -> Expect {
impl From<MockField> for Expect {
fn from(field: MockField) -> Self {
Expect {
fields: HashMap::new(),
only: false,
}
.and(self)
.and(field)
}
}

Expand Down
8 changes: 4 additions & 4 deletions tracing/tests/support/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ impl fmt::Display for MockSpan {
}
}

impl Into<NewSpan> for MockSpan {
fn into(self) -> NewSpan {
NewSpan {
span: self,
impl From<MockSpan> for NewSpan {
fn from(span: MockSpan) -> Self {
Self {
span,
..Default::default()
}
}
Expand Down

0 comments on commit 84ff3a4

Please sign in to comment.