Skip to content

Commit

Permalink
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 28, 2024
1 parent f6960cc commit 6ff1d6a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 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
10 changes: 5 additions & 5 deletions demo/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ impl ReadError {
trait SeekableRead: io::Read + io::Seek {}
impl<T: io::Read + io::Seek> SeekableRead 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);
Expand Down
10 changes: 5 additions & 5 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 @@ -46,8 +46,8 @@ const WRITER_VERSION_DDNET: Version = Version::V6Ddnet;
pub(crate) trait SeekableWrite: io::Write + io::Seek {}
impl<T: io::Write + io::Seek> SeekableWrite 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,7 +57,7 @@ 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 {
Expand Down

0 comments on commit 6ff1d6a

Please sign in to comment.