Skip to content

Commit b74043c

Browse files
Remove the SerializationSink trait.
1 parent b4097fc commit b74043c

File tree

7 files changed

+18
-43
lines changed

7 files changed

+18
-43
lines changed

analyzeme/src/profiling_data.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use measureme::file_header::{
66
read_file_header, write_file_header, CURRENT_FILE_FORMAT_VERSION, FILE_HEADER_SIZE,
77
FILE_MAGIC_EVENT_STREAM,
88
};
9-
use measureme::{
10-
EventId, FileSerializationSink, ProfilerFiles, RawEvent, SerializationSink, StringTableBuilder,
11-
};
9+
use measureme::{EventId, FileSerializationSink, ProfilerFiles, RawEvent, StringTableBuilder};
1210
use serde::{Deserialize, Deserializer};
1311
use std::error::Error;
1412
use std::fs;

measureme/src/file_header.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! All binary files generated by measureme have a simple file header that
22
//! consists of a 4 byte file magic string and a 4 byte little-endian version
33
//! number.
4-
5-
use crate::serialization::SerializationSink;
4+
use crate::FileSerializationSink;
65
use std::convert::TryInto;
76
use std::error::Error;
87

@@ -15,7 +14,7 @@ pub const FILE_MAGIC_STRINGTABLE_INDEX: &[u8; 4] = b"MMSI";
1514
/// rely on this size to be `8`.
1615
pub const FILE_HEADER_SIZE: usize = 8;
1716

18-
pub fn write_file_header<S: SerializationSink>(s: &S, file_magic: &[u8; 4]) {
17+
pub fn write_file_header(s: &FileSerializationSink, file_magic: &[u8; 4]) {
1918
// The implementation here relies on FILE_HEADER_SIZE to have the value 8.
2019
// Let's make sure this assumption cannot be violated without being noticed.
2120
assert_eq!(FILE_HEADER_SIZE, 8);

measureme/src/file_serialization_sink.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::serialization::{Addr, SerializationSink};
1+
use crate::serialization::Addr;
22
use parking_lot::Mutex;
33
use std::error::Error;
44
use std::fmt::Debug;
@@ -69,10 +69,8 @@ impl FileSerializationSink {
6969
// Then we can create a copy of the data written so far.
7070
file.drain_bytes()
7171
}
72-
}
7372

74-
impl SerializationSink for FileSerializationSink {
75-
fn from_path(path: &Path) -> Result<Self, Box<dyn Error + Send + Sync>> {
73+
pub fn from_path(path: &Path) -> Result<Self, Box<dyn Error + Send + Sync>> {
7674
fs::create_dir_all(path.parent().unwrap())?;
7775

7876
let file = fs::File::create(path)?;
@@ -88,7 +86,7 @@ impl SerializationSink for FileSerializationSink {
8886
}
8987

9088
#[inline]
91-
fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr
89+
pub fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr
9290
where
9391
W: FnOnce(&mut [u8]),
9492
{
@@ -131,7 +129,7 @@ impl SerializationSink for FileSerializationSink {
131129
Addr(curr_addr)
132130
}
133131

134-
fn write_bytes_atomic(&self, bytes: &[u8]) -> Addr {
132+
pub fn write_bytes_atomic(&self, bytes: &[u8]) -> Addr {
135133
if bytes.len() < 128 {
136134
// For "small" pieces of data, use the regular implementation so we
137135
// don't repeatedly flush an almost empty buffer to disk.

measureme/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ pub use crate::file_serialization_sink::FileSerializationSink;
5656
#[cfg(not(target_arch = "wasm32"))]
5757
pub use crate::profiler::{Profiler, ProfilerFiles, TimingGuard};
5858
pub use crate::raw_event::{RawEvent, MAX_INSTANT_TIMESTAMP, MAX_INTERVAL_TIMESTAMP};
59-
pub use crate::serialization::{Addr, SerializationSink};
59+
pub use crate::serialization::Addr;
6060
pub use crate::stringtable::{SerializableString, StringComponent, StringId, StringTableBuilder};

measureme/src/profiler.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::event_id::EventId;
22
use crate::file_header::{write_file_header, FILE_MAGIC_EVENT_STREAM};
33
use crate::file_serialization_sink::FileSerializationSink;
44
use crate::raw_event::RawEvent;
5-
use crate::serialization::SerializationSink;
65
use crate::stringtable::{SerializableString, StringId, StringTableBuilder};
76
use std::error::Error;
87
use std::path::{Path, PathBuf};
@@ -34,14 +33,14 @@ pub struct Profiler {
3433
impl Profiler {
3534
pub fn new<P: AsRef<Path>>(path_stem: P) -> Result<Profiler, Box<dyn Error + Send + Sync>> {
3635
let paths = ProfilerFiles::new(path_stem.as_ref());
37-
let event_sink = Arc::new(SerializationSink::from_path(&paths.events_file)?);
36+
let event_sink = Arc::new(FileSerializationSink::from_path(&paths.events_file)?);
3837

3938
// The first thing in every file we generate must be the file header.
4039
write_file_header(&*event_sink, FILE_MAGIC_EVENT_STREAM);
4140

4241
let string_table = StringTableBuilder::new(
43-
Arc::new(SerializationSink::from_path(&paths.string_data_file)?),
44-
Arc::new(SerializationSink::from_path(&paths.string_index_file)?),
42+
Arc::new(FileSerializationSink::from_path(&paths.string_data_file)?),
43+
Arc::new(FileSerializationSink::from_path(&paths.string_index_file)?),
4544
);
4645

4746
let profiler = Profiler {
@@ -167,3 +166,9 @@ impl<'a> TimingGuard<'a> {
167166
drop(self)
168167
}
169168
}
169+
170+
// Make sure that `Profiler` can be used in a multithreaded context
171+
fn _assert_bounds() {
172+
assert_bounds_inner(&Profiler::new(""));
173+
fn assert_bounds_inner<S: Sized + Send + Sync + 'static>(_: &S) {}
174+
}

measureme/src/serialization.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use std::error::Error;
2-
use std::path::Path;
3-
41
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
52
pub struct Addr(pub u32);
63

@@ -9,25 +6,3 @@ impl Addr {
96
self.0 as usize
107
}
118
}
12-
13-
pub trait SerializationSink: Sized + Send + Sync + 'static {
14-
fn from_path(path: &Path) -> Result<Self, Box<dyn Error + Send + Sync>>;
15-
16-
/// Atomically write `num_bytes` to the sink. The implementation must ensure
17-
/// that concurrent invocations of `write_atomic` do not conflict with each
18-
/// other.
19-
///
20-
/// The `write` argument is a function that must fill the output buffer
21-
/// passed to it. The output buffer is guaranteed to be exactly `num_bytes`
22-
/// large.
23-
fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr
24-
where
25-
W: FnOnce(&mut [u8]);
26-
27-
/// Same as write_atomic() but might be faster in cases where bytes to be
28-
/// written are already present in a buffer (as opposed to when it is
29-
/// benefical to directly serialize into the output buffer).
30-
fn write_bytes_atomic(&self, bytes: &[u8]) -> Addr {
31-
self.write_atomic(bytes.len(), |sink| sink.copy_from_slice(bytes))
32-
}
33-
}

measureme/src/stringtable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use crate::file_header::{
6767
write_file_header, FILE_MAGIC_STRINGTABLE_DATA, FILE_MAGIC_STRINGTABLE_INDEX,
6868
};
6969
use crate::file_serialization_sink::FileSerializationSink;
70-
use crate::serialization::{Addr, SerializationSink};
70+
use crate::serialization::Addr;
7171
use std::sync::Arc;
7272

7373
/// A `StringId` is used to identify a string in the `StringTable`. It is

0 commit comments

Comments
 (0)