-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract TraceBackend as a trait
- Loading branch information
Jason
committed
Sep 8, 2024
1 parent
c41679f
commit ffe1566
Showing
16 changed files
with
278 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use std::collections::VecDeque; | ||
use std::path::PathBuf; | ||
|
||
use specs::etable::EventTable; | ||
use specs::external_host_call_table::ExternalHostCallTable; | ||
use specs::jtable::FrameTable; | ||
use specs::slice_backend::Slice; | ||
use specs::slice_backend::SliceBackend; | ||
|
||
use crate::names::name_of_etable_slice; | ||
use crate::names::name_of_external_host_call_table_slice; | ||
use crate::names::name_of_frame_table_slice; | ||
|
||
struct SlicePath { | ||
event_table: PathBuf, | ||
frame_table: PathBuf, | ||
external_host_call_table: PathBuf, | ||
} | ||
|
||
impl Into<Slice> for &SlicePath { | ||
fn into(self) -> Slice { | ||
Slice { | ||
etable: EventTable::read(&self.event_table).unwrap(), | ||
frame_table: FrameTable::read(&self.frame_table).unwrap(), | ||
external_host_call_table: ExternalHostCallTable::read(&self.external_host_call_table) | ||
.unwrap(), | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct FileBackend { | ||
dir_path: PathBuf, | ||
name: String, | ||
slices: VecDeque<SlicePath>, | ||
} | ||
|
||
impl FileBackend { | ||
pub(crate) fn new(name: String, dir_path: PathBuf) -> Self { | ||
FileBackend { | ||
dir_path, | ||
name, | ||
slices: VecDeque::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Iterator for FileBackend { | ||
type Item = Slice; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.slices.pop_front().map(|slice| (&slice).into()) | ||
} | ||
} | ||
|
||
impl SliceBackend for FileBackend { | ||
fn push(&mut self, slice: Slice) { | ||
let index = self.slices.len(); | ||
|
||
let event_table = { | ||
let path = self | ||
.dir_path | ||
.join(PathBuf::from(name_of_etable_slice(&self.name, index))); | ||
slice.etable.write(&path).unwrap(); | ||
path | ||
}; | ||
|
||
let frame_table = { | ||
let path = self | ||
.dir_path | ||
.join(PathBuf::from(name_of_frame_table_slice(&self.name, index))); | ||
slice.frame_table.write(&path).unwrap(); | ||
path | ||
}; | ||
|
||
let external_host_call_table = { | ||
let path = self | ||
.dir_path | ||
.join(PathBuf::from(name_of_external_host_call_table_slice( | ||
&self.name, index, | ||
))); | ||
slice.external_host_call_table.write(&path).unwrap(); | ||
path | ||
}; | ||
|
||
self.slices.push_back(SlicePath { | ||
event_table, | ||
frame_table, | ||
external_host_call_table, | ||
}); | ||
} | ||
|
||
fn len(&self) -> usize { | ||
self.slices.len() | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
self.slices.is_empty() | ||
} | ||
|
||
fn for_each1<'a>(&'a self, f: Box<dyn Fn((usize, &Slice)) + 'a>) { | ||
self.slices.iter().enumerate().for_each(|(index, slice)| { | ||
let slice: Slice = slice.into(); | ||
f((index, &slice)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.