Skip to content

Commit

Permalink
fix downcast_raw error when using with_span_field_formatter (#15)
Browse files Browse the repository at this point in the history
with_span_field_formatter changes the generic types of the returned `Self`, so it's not valid to pass through `self.get_context`; we instead need to refetch `get_context` based on the new type. added a unit tests that validates this works.
  • Loading branch information
emersonford committed Jan 18, 2025
1 parent c70d4e8 commit bf36bcc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,11 @@ where
}

// pub methods
impl<S, F> IndicatifLayer<S, F> {
impl<S, F> IndicatifLayer<S, F>
where
S: Subscriber + for<'a> LookupSpan<'a>,
F: for<'writer> FormatFields<'writer> + 'static,
{
#[deprecated(since = "0.2.3", note = "use get_stderr_writer() instead")]
pub fn get_fmt_writer(&self) -> IndicatifWriter<writer::Stderr> {
self.get_stderr_writer()
Expand Down Expand Up @@ -443,10 +447,16 @@ impl<S, F> IndicatifLayer<S, F> {
progress_style: self.progress_style,
span_child_prefix_indent: self.span_child_prefix_indent,
span_child_prefix_symbol: self.span_child_prefix_symbol,
get_context: self.get_context,
get_stderr_writer_context: self.get_stderr_writer_context,
get_stdout_writer_context: self.get_stdout_writer_context,
get_multi_progress_context: self.get_multi_progress_context,
get_context: WithContext(IndicatifLayer::<S, F2>::get_context),
get_stderr_writer_context: WithStderrWriter(
IndicatifLayer::<S, F2>::get_stderr_writer_context,
),
get_stdout_writer_context: WithStdoutWriter(
IndicatifLayer::<S, F2>::get_stdout_writer_context,
),
get_multi_progress_context: WithMultiProgress(
IndicatifLayer::<S, F2>::get_multi_progress_context,
),
inner: self.inner,
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tracing_subscriber::fmt::format::DefaultFields;
use tracing_subscriber::fmt::MakeWriter;
use tracing_subscriber::layer::SubscriberExt;

use crate::filter::hide_indicatif_span_fields;
use crate::span_ext::IndicatifSpanExt;
use crate::suspend_tracing_indicatif;
use crate::IndicatifLayer;
Expand Down Expand Up @@ -858,6 +859,23 @@ hello world
);
}

#[test]
fn test_with_span_field_formatter() {
let indicatif_layer = IndicatifLayer::new()
.with_span_field_formatter(hide_indicatif_span_fields(DefaultFields::new()));

let subscriber = tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_writer(indicatif_layer.get_stderr_writer()))
.with(indicatif_layer);

tracing::subscriber::with_default(subscriber, || {
let _span = info_span!("foo");
_span.pb_start();

suspend_tracing_indicatif(|| {});
});
}

// These don't actually run anything, but exist to type check macros.
#[allow(dead_code)]
fn type_check_indicatif_println() {
Expand Down

0 comments on commit bf36bcc

Please sign in to comment.