Skip to content

Commit f680868

Browse files
authored
Don't use Arc in Settings unnecessarily (#873)
1 parent 1324590 commit f680868

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ target
1010
BUILD.bazel
1111
WORKSPACE.bazel
1212
bazel-*
13+
14+
# Emacs backups
15+
*~

insta/src/settings.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use once_cell::sync::Lazy;
21
#[cfg(feature = "serde")]
32
use serde::{de::value::Error as ValueError, Serialize};
43
use std::cell::RefCell;
54
use std::future::Future;
65
use std::mem;
76
use std::path::{Path, PathBuf};
87
use std::pin::Pin;
9-
use std::sync::Arc;
8+
use std::rc::Rc;
109
use std::task::{Context, Poll};
1110

1211
use crate::content::Content;
@@ -17,40 +16,21 @@ use crate::filters::Filters;
1716
#[cfg(feature = "redactions")]
1817
use crate::redaction::{dynamic_redaction, sorted_redaction, ContentPath, Redaction, Selector};
1918

20-
static DEFAULT_SETTINGS: Lazy<Arc<ActualSettings>> = Lazy::new(|| {
21-
Arc::new(ActualSettings {
22-
sort_maps: false,
23-
snapshot_path: "snapshots".into(),
24-
snapshot_suffix: "".into(),
25-
input_file: None,
26-
description: None,
27-
info: None,
28-
omit_expression: false,
29-
prepend_module_to_snapshot: true,
30-
#[cfg(feature = "redactions")]
31-
redactions: Redactions::default(),
32-
#[cfg(feature = "filters")]
33-
filters: Filters::default(),
34-
#[cfg(feature = "glob")]
35-
allow_empty_glob: false,
36-
})
37-
});
38-
3919
thread_local!(static CURRENT_SETTINGS: RefCell<Settings> = RefCell::new(Settings::new()));
4020

4121
/// Represents stored redactions.
4222
#[cfg(feature = "redactions")]
4323
#[cfg_attr(docsrs, doc(cfg(feature = "redactions")))]
4424
#[derive(Clone, Default)]
45-
pub struct Redactions(Vec<(Selector<'static>, Arc<Redaction>)>);
25+
pub struct Redactions(Vec<(Selector<'static>, Rc<Redaction>)>);
4626

4727
#[cfg(feature = "redactions")]
4828
impl<'a> From<Vec<(&'a str, Redaction)>> for Redactions {
4929
fn from(value: Vec<(&'a str, Redaction)>) -> Redactions {
5030
Redactions(
5131
value
5232
.into_iter()
53-
.map(|x| (Selector::parse(x.0).unwrap().make_static(), Arc::new(x.1)))
33+
.map(|x| (Selector::parse(x.0).unwrap().make_static(), Rc::new(x.1)))
5434
.collect(),
5535
)
5636
}
@@ -181,13 +161,28 @@ impl ActualSettings {
181161
/// ```
182162
#[derive(Clone)]
183163
pub struct Settings {
184-
inner: Arc<ActualSettings>,
164+
inner: Rc<ActualSettings>,
185165
}
186166

187167
impl Default for Settings {
188168
fn default() -> Settings {
189169
Settings {
190-
inner: DEFAULT_SETTINGS.clone(),
170+
inner: Rc::new(ActualSettings {
171+
sort_maps: false,
172+
snapshot_path: "snapshots".into(),
173+
snapshot_suffix: "".into(),
174+
input_file: None,
175+
description: None,
176+
info: None,
177+
omit_expression: false,
178+
prepend_module_to_snapshot: true,
179+
#[cfg(feature = "redactions")]
180+
redactions: Redactions::default(),
181+
#[cfg(feature = "filters")]
182+
filters: Filters::default(),
183+
#[cfg(feature = "glob")]
184+
allow_empty_glob: false,
185+
}),
191186
}
192187
}
193188
}
@@ -209,7 +204,7 @@ impl Settings {
209204
/// Internal helper for macros
210205
#[doc(hidden)]
211206
pub fn _private_inner_mut(&mut self) -> &mut ActualSettings {
212-
Arc::make_mut(&mut self.inner)
207+
Rc::make_mut(&mut self.inner)
213208
}
214209

215210
/// Enables forceful sorting of maps before serialization.
@@ -402,7 +397,7 @@ impl Settings {
402397
fn add_redaction_impl(&mut self, selector: &str, replacement: Redaction) {
403398
self._private_inner_mut().redactions.0.push((
404399
Selector::parse(selector).unwrap().make_static(),
405-
Arc::new(replacement),
400+
Rc::new(replacement),
406401
));
407402
}
408403

@@ -551,7 +546,7 @@ impl Settings {
551546
/// ```
552547
pub fn bind_async<F: Future<Output = T>, T>(&self, future: F) -> impl Future<Output = T> {
553548
struct BindingFuture<F> {
554-
settings: Arc<ActualSettings>,
549+
settings: Rc<ActualSettings>,
555550
future: F,
556551
}
557552

@@ -627,7 +622,7 @@ impl Settings {
627622
/// This is to ensure tests under async runtimes like `tokio` don't show unexpected results
628623
#[must_use = "The guard is immediately dropped so binding has no effect. Use `let _guard = ...` to bind it."]
629624
pub struct SettingsBindDropGuard(
630-
Option<Arc<ActualSettings>>,
625+
Option<Rc<ActualSettings>>,
631626
/// A ZST that is not [`Send`] but is [`Sync`]
632627
///
633628
/// This is necessary due to the lack of stable [negative impls](https://github.com/rust-lang/rust/issues/68318).

0 commit comments

Comments
 (0)