Skip to content

Commit

Permalink
Change OnceCell to OnceLock in TabExpandedString
Browse files Browse the repository at this point in the history
The change at [1] stores some data in a `OnceCell`, which made
`ProgressStyle` no longer `Sync`. Fix this by changing the `OnceCell` to
a `OnceLock`, and add a static test that all relevant public types are
`Send` and `Sync`.

[1]: #684
  • Loading branch information
tgross35 authored and djc committed Jan 28, 2025
1 parent c340e9f commit 9d4849a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,21 @@ pub use crate::rayon::ParallelProgressIterator;
pub use crate::state::{ProgressFinish, ProgressState};
pub use crate::style::ProgressStyle;
pub use crate::term_like::TermLike;

#[cfg(test)]
mod tests {
use super::*;

#[allow(dead_code)]
trait MustBeThreadSafe: Send + Sync {}

// Ensure that the following types are `Send + Sync`
impl MustBeThreadSafe for MultiProgress {}
impl MustBeThreadSafe for MultiProgressAlignment {}
impl MustBeThreadSafe for ProgressBar {}
impl MustBeThreadSafe for ProgressBarIter<()> {}
impl MustBeThreadSafe for ProgressFinish {}
impl MustBeThreadSafe for ProgressState {}
impl MustBeThreadSafe for ProgressStyle {}
impl MustBeThreadSafe for WeakProgressBar {}
}
7 changes: 3 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::borrow::Cow;
use std::cell::OnceCell;
use std::io;
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
Expand Down Expand Up @@ -355,7 +354,7 @@ pub(crate) enum TabExpandedString {
NoTabs(Cow<'static, str>),
WithTabs {
original: Cow<'static, str>,
expanded: OnceCell<String>,
expanded: OnceLock<String>,
tab_width: usize,
},
}
Expand All @@ -368,7 +367,7 @@ impl TabExpandedString {
Self::WithTabs {
original: s,
tab_width,
expanded: OnceCell::new(),
expanded: OnceLock::new(),
}
}
}
Expand Down

0 comments on commit 9d4849a

Please sign in to comment.