Skip to content

Commit 617dff5

Browse files
committed
API: Take AsRef<Path> and Send+Sync errors
Having errors be `Send+Sync` is a good practice in the Rust ecosystem as it allows at least passing the errors in and out of threads as it is propagated. `AsRef<Path>` allows more convenient use of the constructor wherein you have the convenience of being able to pass in e.g. a string literal.
1 parent 7ff08df commit 617dff5

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

measureme/src/file_serialization_sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct Inner {
1717
}
1818

1919
impl SerializationSink for FileSerializationSink {
20-
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>> {
20+
fn from_path(path: &Path) -> Result<Self, Box<dyn Error + Send + Sync>> {
2121
fs::create_dir_all(path.parent().unwrap())?;
2222

2323
let file = fs::File::create(path)?;

measureme/src/mmap_serialization_sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct MmapSerializationSink {
1313
}
1414

1515
impl SerializationSink for MmapSerializationSink {
16-
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>> {
16+
fn from_path(path: &Path) -> Result<Self, Box<dyn Error + Send + Sync>> {
1717
// Lazily allocate 1 GB :O
1818
let file_size = 1 << 30;
1919

measureme/src/profiler.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ pub struct ProfilerFiles {
1515
}
1616

1717
impl ProfilerFiles {
18-
pub fn new(path_stem: &Path) -> ProfilerFiles {
18+
pub fn new<P: AsRef<Path>>(path_stem: P) -> ProfilerFiles {
1919
ProfilerFiles {
20-
events_file: path_stem.with_extension("events"),
21-
string_data_file: path_stem.with_extension("string_data"),
22-
string_index_file: path_stem.with_extension("string_index"),
20+
events_file: path_stem.as_ref().with_extension("events"),
21+
string_data_file: path_stem.as_ref().with_extension("string_data"),
22+
string_index_file: path_stem.as_ref().with_extension("string_index"),
2323
}
2424
}
2525
}
@@ -31,8 +31,9 @@ pub struct Profiler<S: SerializationSink> {
3131
}
3232

3333
impl<S: SerializationSink> Profiler<S> {
34-
pub fn new(path_stem: &Path) -> Result<Profiler<S>, Box<dyn Error>> {
35-
let paths = ProfilerFiles::new(path_stem);
34+
pub fn new<P: AsRef<Path>>(path_stem: P)
35+
-> Result<Profiler<S>, Box<dyn Error + Send + Sync>> {
36+
let paths = ProfilerFiles::new(path_stem.as_ref());
3637
let event_sink = Arc::new(S::from_path(&paths.events_file)?);
3738

3839
// The first thing in every file we generate must be the file header.

measureme/src/serialization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl Addr {
1212
}
1313

1414
pub trait SerializationSink: Sized + Send + Sync + 'static {
15-
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>>;
15+
fn from_path(path: &Path) -> Result<Self, Box<dyn Error + Send + Sync>>;
1616

1717
/// Atomically write `num_bytes` to the sink. The implementation must ensure
1818
/// that concurrent invocations of `write_atomic` do not conflict with each
@@ -53,7 +53,7 @@ impl ByteVecSink {
5353
}
5454

5555
impl SerializationSink for ByteVecSink {
56-
fn from_path(_path: &Path) -> Result<Self, Box<dyn Error>> {
56+
fn from_path(_path: &Path) -> Result<Self, Box<dyn Error + Send + Sync>> {
5757
unimplemented!()
5858
}
5959

0 commit comments

Comments
 (0)