Skip to content

Commit

Permalink
[RFC] demo: Replace 'static with explicit lifetime 'a
Browse files Browse the repository at this point in the history
Previously only owned readers / writers could be used.
Now, `&mut Vec<u8>` can be used.

Also minor formatting changes.
  • Loading branch information
Patiga committed May 16, 2024
1 parent f6960cc commit 1703cab
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
10 changes: 5 additions & 5 deletions demo/src/ddnet/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub enum ReadError {
ChunkOrder,
}

pub struct DemoReader<P: for<'a> Protocol<'a>> {
raw: reader::Reader,
pub struct DemoReader<'a, P: for<'p> Protocol<'p>> {
raw: reader::Reader<'a>,
delta: Delta,
snap: Snap,
old_snap: Snap,
Expand Down Expand Up @@ -84,10 +84,10 @@ pub enum Chunk<'a, P: Protocol<'a>> {
Invalid,
}

impl<P: for<'a> Protocol<'a>> DemoReader<P> {
impl<'a, P: for<'p> Protocol<'p>> DemoReader<'a, P> {
pub fn new<R, W>(data: R, warn: &mut W) -> Result<Self, ReadError>
where
R: io::Read + io::Seek + 'static,
R: io::Read + io::Seek + 'a,
W: Warn<Warning>,
{
let reader = reader::Reader::new(data, wrap(warn))?;
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<P: for<'a> Protocol<'a>> DemoReader<P> {
}
}

pub fn inner(&self) -> &reader::Reader {
pub fn inner(&'a self) -> &'a reader::Reader {
&self.raw
}
}
Expand Down
10 changes: 5 additions & 5 deletions demo/src/ddnet/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl From<snap::BuilderError> for WriteError {
/// DDNet demo writer.
///
/// Automatically writes snapshot deltas.
pub struct DemoWriter<P: for<'a> Protocol<'a>> {
inner: crate::Writer,
pub struct DemoWriter<'a, P: for<'p> Protocol<'p>> {
inner: crate::Writer<'a>,
// To verify the monotonic increase
last_tick: i32,
// Stores the last tick, in which a snapshot was written.
Expand Down Expand Up @@ -94,8 +94,8 @@ impl UuidIndex {
}
}

impl<P: for<'a> Protocol<'a>> DemoWriter<P> {
pub fn new<T: io::Write + io::Seek + 'static>(
impl<'a, P: for<'p> Protocol<'p>> DemoWriter<'a, P> {
pub fn new<T: io::Write + io::Seek + 'a>(
file: T,
net_version: &[u8],
map_name: &[u8],
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<P: for<'a> Protocol<'a>> DemoWriter<P> {
})
}

pub fn write_snap<'a, T: Iterator<Item = (&'a P::SnapObj, u16)>>(
pub fn write_snap<'b, T: Iterator<Item = (&'b P::SnapObj, u16)>>(
&mut self,
tick: i32,
items: T,
Expand Down
23 changes: 10 additions & 13 deletions demo/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ impl ReadError {
}
}

trait SeekableRead: io::Read + io::Seek {}
impl<T: io::Read + io::Seek> SeekableRead for T {}
trait SeekableRead<'a>: io::Read + io::Seek + 'a {}
impl<'a, T: io::Read + io::Seek + 'a> SeekableRead<'a> for T {}

pub struct Reader {
data: Box<dyn SeekableRead>,
pub struct Reader<'a> {
data: Box<dyn SeekableRead<'a>>,
start: format::HeaderStart,
current_tick: Option<i32>,
raw: [u8; MAX_SNAPSHOT_SIZE],
huffman: ArrayVec<[u8; MAX_SNAPSHOT_SIZE]>,
}

impl Reader {
pub fn new<W, R>(mut data: R, warn: &mut W) -> Result<Reader, ReadError>
impl<'a> Reader<'a> {
pub fn new<W, R>(mut data: R, warn: &mut W) -> Result<Reader<'a>, ReadError>
where
W: Warn<Warning>,
R: io::Read + io::Seek + 'static,
R: io::Read + io::Seek + 'a,
{
let start = format::HeaderStart::read(&mut data)?;
start.header.check(warn);
start.timeline_markers.check(warn);
Ok(Self {
data: Box::new(data),
start: start,
start,
current_tick: None,
raw: [0; MAX_SNAPSHOT_SIZE],
huffman: ArrayVec::new(),
Expand Down Expand Up @@ -129,10 +129,7 @@ impl Reader {
}
}
self.current_tick = Some(t);
Ok(Some(RawChunk::Tick {
tick: t,
keyframe: keyframe,
}))
Ok(Some(RawChunk::Tick { tick: t, keyframe }))
}
ChunkHeader::Tick {
marker: TickMarker::Delta(d),
Expand All @@ -145,7 +142,7 @@ impl Reader {
self.current_tick = Some(new_t);
Ok(Some(RawChunk::Tick {
tick: new_t,
keyframe: keyframe,
keyframe,
}))
}
},
Expand Down
20 changes: 10 additions & 10 deletions demo/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl WriteError {
}
}

pub struct Writer {
file: Box<dyn SeekableWrite>,
pub struct Writer<'a> {
file: Box<dyn SeekableWrite<'a>>,
header: Header,
prev_tick: Option<i32>,
huffman: ArrayVec<[u8; MAX_SNAPSHOT_SIZE]>,
Expand All @@ -43,11 +43,11 @@ pub struct Writer {
const WRITER_VERSION: Version = Version::V5;
const WRITER_VERSION_DDNET: Version = Version::V6Ddnet;

pub(crate) trait SeekableWrite: io::Write + io::Seek {}
impl<T: io::Write + io::Seek> SeekableWrite for T {}
pub(crate) trait SeekableWrite<'a>: io::Write + io::Seek + 'a {}
impl<'a, T: io::Write + io::Seek + 'a> SeekableWrite<'a> for T {}

impl Writer {
pub fn new<W: io::Write + io::Seek + 'static>(
impl<'a> Writer<'a> {
pub fn new<W: io::Write + io::Seek + 'a>(
file: W,
net_version: &[u8],
map_name: &[u8],
Expand All @@ -57,15 +57,15 @@ impl Writer {
length: i32,
timestamp: &[u8],
map: &[u8],
) -> Result<Writer, WriteError> {
) -> Result<Writer<'a>, WriteError> {
let mut writer = Writer {
file: Box::new(file),
header: Header {
net_version: CappedString::from_raw(net_version),
map_name: CappedString::from_raw(map_name),
map_size: map.len().assert_i32(),
map_crc: map_crc,
kind: kind,
map_crc,
kind,
length,
timestamp: CappedString::from_raw(timestamp),
},
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Writer {
let tm = TickMarker::new(tick, self.prev_tick, keyframe, WRITER_VERSION);
ChunkHeader::Tick {
marker: tm,
keyframe: keyframe,
keyframe,
}
.write(&mut self.file, WRITER_VERSION)?;
self.prev_tick = Some(tick);
Expand Down
3 changes: 2 additions & 1 deletion tools/src/bin/teehistorian2demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ fn process(in_: &Path, out: &Path) -> Result<(), String> {
let mut snap_buffer = Vec::new();
let mut th;
let mut demo;
let mut outt = std::io::Cursor::new(Vec::new());
{
let (header, teehistorian) =
Reader::open(in_, &mut buffer).map_err(|err| format!("{:?}", err))?;
th = teehistorian;
let file = fs::File::create(out).map_err(|err| err.to_string())?;
demo = Writer::new(
file,
&mut outt,
VERSION.as_bytes(),
header.map_name.as_bytes(),
header.map_sha256,
Expand Down

0 comments on commit 1703cab

Please sign in to comment.