Skip to content

Commit 5ca1388

Browse files
wesleywisermichaelwoerister
authored andcommitted
Implement better error handling
Fixes #9
1 parent 5c7eedb commit 5ca1388

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

measureme/src/file_serialization_sink.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::serialization::{Addr, SerializationSink};
2+
use std::error::Error;
23
use std::fs;
34
use std::io::{BufWriter, Write};
45
use std::path::Path;
@@ -9,16 +10,14 @@ pub struct FileSerializationSink {
910
}
1011

1112
impl SerializationSink for FileSerializationSink {
12-
fn from_path(path: &Path) -> Self {
13-
// TODO: This is very crude error handling.
14-
// https://github.com/rust-lang/measureme/issues/9
15-
fs::create_dir_all(path.parent().unwrap()).unwrap();
13+
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>> {
14+
fs::create_dir_all(path.parent().unwrap())?;
1615

17-
let file = fs::File::create(path).expect("couldn't open file: {}");
16+
let file = fs::File::create(path)?;
1817

19-
FileSerializationSink {
18+
Ok(FileSerializationSink {
2019
data: Mutex::new((BufWriter::new(file), 0)),
21-
}
20+
})
2221
}
2322

2423
#[inline]

measureme/src/mmap_serialization_sink.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::serialization::{Addr, SerializationSink};
22
use memmap::{MmapMut};
3+
use std::error::Error;
34
use std::io::{Write, BufWriter};
45
use std::fs::{File};
56
use std::path::{Path, PathBuf};
@@ -12,17 +13,17 @@ pub struct MmapSerializationSink {
1213
}
1314

1415
impl SerializationSink for MmapSerializationSink {
15-
fn from_path(path: &Path) -> Self {
16+
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>> {
1617
// Lazily allocate 1 GB :O
1718
let file_size = 1 << 30;
1819

19-
let mapped_file = MmapMut::map_anon(file_size).unwrap();
20+
let mapped_file = MmapMut::map_anon(file_size)?;
2021

21-
MmapSerializationSink {
22+
Ok(MmapSerializationSink {
2223
mapped_file,
2324
current_pos: AtomicUsize::new(0),
2425
path: path.to_path_buf(),
25-
}
26+
})
2627
}
2728

2829
#[inline]

measureme/src/profiler.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::raw_event::{RawEvent, Timestamp, TimestampKind};
22
use crate::serialization::SerializationSink;
33
use crate::stringtable::{SerializableString, StringId, StringTableBuilder};
4+
use std::error::Error;
45
use std::path::{Path, PathBuf};
56
use std::sync::Arc;
67
use std::time::Instant;
@@ -28,19 +29,19 @@ pub struct Profiler<S: SerializationSink> {
2829
}
2930

3031
impl<S: SerializationSink> Profiler<S> {
31-
pub fn new(path_stem: &Path) -> Profiler<S> {
32+
pub fn new(path_stem: &Path) -> Result<Profiler<S>, Box<dyn Error>> {
3233
let paths = ProfilerFiles::new(path_stem);
33-
let event_sink = Arc::new(S::from_path(&paths.events_file));
34+
let event_sink = Arc::new(S::from_path(&paths.events_file)?);
3435
let string_table = StringTableBuilder::new(
35-
Arc::new(S::from_path(&paths.string_data_file)),
36-
Arc::new(S::from_path(&paths.string_index_file)),
36+
Arc::new(S::from_path(&paths.string_data_file)?),
37+
Arc::new(S::from_path(&paths.string_index_file)?),
3738
);
3839

39-
Profiler {
40+
Ok(Profiler {
4041
event_sink,
4142
string_table,
4243
start_time: Instant::now(),
43-
}
44+
})
4445
}
4546

4647
#[inline(always)]

measureme/src/serialization.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::error::Error;
12
use std::path::Path;
23

34
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
@@ -9,8 +10,8 @@ impl Addr {
910
}
1011
}
1112

12-
pub trait SerializationSink {
13-
fn from_path(path: &Path) -> Self;
13+
pub trait SerializationSink: Sized {
14+
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>>;
1415

1516
fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr
1617
where
@@ -39,7 +40,7 @@ pub mod test {
3940
}
4041

4142
impl SerializationSink for TestSink {
42-
fn from_path(_path: &Path) -> Self {
43+
fn from_path(_path: &Path) -> Result<Self, Box<dyn Error>> {
4344
unimplemented!()
4445
}
4546

measureme/src/testing_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn mk_filestem(file_name_stem: &str) -> PathBuf {
2222

2323
// Generate some profiling data. This is the part that would run in rustc.
2424
fn generate_profiling_data<S: SerializationSink>(filestem: &Path) -> Vec<Event> {
25-
let profiler = Arc::new(Profiler::<S>::new(Path::new(filestem)));
25+
let profiler = Arc::new(Profiler::<S>::new(Path::new(filestem)).unwrap());
2626

2727
let event_id_reserved = StringId::reserved(42);
2828

0 commit comments

Comments
 (0)