From ec77fda0944510bc7c05ec64105375e31bd8819b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 22 Feb 2024 15:34:08 +0200 Subject: [PATCH] Clean up lifetimes of packet builder write machinery --- src/builder.rs | 52 ++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index c0782df..fc15e4b 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,5 +1,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 +use std::marker::PhantomData; + use crate::RtpPacket; /// Errors produced when wrting a packet @@ -267,30 +269,30 @@ impl RtpPacketBuilder { } } -impl<'a> RtpPacketBuilder<&'a [u8], &'a [u8]> { +impl<'a, 'b> RtpPacketBuilder<&'a [u8], &'b [u8]> { /// Write this packet into `buf` without any validity checks. Returns the number of bytes /// written. - pub fn write_into_unchecked<'b: 'a>(&self, buf: &'b mut [u8]) -> usize { + pub fn write_into_unchecked(&self, buf: &mut [u8]) -> usize { let mut writer = RtpPacketWriterMutSlice::new(buf); self.write_unchecked(&mut writer) } /// Write this packet into `buf`. On success returns the number of bytes written or an /// `RtpWriteError` on failure. - pub fn write_into<'b: 'a>(&self, buf: &'b mut [u8]) -> Result { + pub fn write_into(&self, buf: &mut [u8]) -> Result { let mut writer = RtpPacketWriterMutSlice::new(buf); self.write(&mut writer) } /// Write this packet into `buf` without any validity checks. The data will be appended to the /// end of the provide Vec. - pub fn write_into_vec_unchecked(&self, buf: &'a mut Vec) { + pub fn write_into_vec_unchecked(&self, buf: &mut Vec) { let mut writer = RtpPacketWriterMutVec::new(buf); self.write_unchecked(&mut writer) } /// Write this packet into `buf`. The data will be appended to the end of the provide Vec. - pub fn write_into_vec(&self, buf: &'a mut Vec) -> Result<(), RtpWriteError> { + pub fn write_into_vec(&self, buf: &mut Vec) -> Result<(), RtpWriteError> { let mut writer = RtpPacketWriterMutVec::new(buf); self.write(&mut writer) } @@ -381,16 +383,16 @@ impl PayloadLength for &[T; N] { /// An implementation of a [`RtpPacketWriter`] that appends to a `Vec`. #[derive(Default, Debug)] -pub struct RtpPacketWriterVec<'a> { +pub struct RtpPacketWriterVec<'a, 'b> { output: Vec, padding: Option, - phantom: std::marker::PhantomData<&'a [u8]>, + phantom: PhantomData<(&'a [u8], &'b [u8])>, } -impl<'a> RtpPacketWriter for RtpPacketWriterVec<'a> { +impl<'a, 'b> RtpPacketWriter for RtpPacketWriterVec<'a, 'b> { type Output = Vec; type Payload = &'a [u8]; - type Extension = &'a [u8]; + type Extension = &'b [u8]; fn reserve(&mut self, size: usize) { if self.output.len() < size { @@ -429,23 +431,25 @@ impl<'a> RtpPacketWriter for RtpPacketWriterVec<'a> { /// An implementation of a [`RtpPacketWriter`] that writes to a `&mut [u8]`. Each packet will be /// written starting at the beginning of the provided slice. #[derive(Default, Debug)] -pub struct RtpPacketWriterMutSlice<'a> { +pub struct RtpPacketWriterMutSlice<'a, 'b, 'c> { output: &'a mut [u8], padding: Option, write_i: usize, + phantom: PhantomData<(&'b [u8], &'c [u8])>, } -impl<'a> RtpPacketWriterMutSlice<'a> { +impl<'a, 'b, 'c> RtpPacketWriterMutSlice<'a, 'b, 'c> { pub fn new(buf: &'a mut [u8]) -> Self { Self { output: buf, padding: None, write_i: 0, + phantom: PhantomData, } } } -impl<'a> std::ops::Deref for RtpPacketWriterMutSlice<'a> { +impl<'a, 'b, 'c> std::ops::Deref for RtpPacketWriterMutSlice<'a, 'b, 'c> { type Target = [u8]; fn deref(&self) -> &Self::Target { @@ -453,16 +457,16 @@ impl<'a> std::ops::Deref for RtpPacketWriterMutSlice<'a> { } } -impl<'a> std::ops::DerefMut for RtpPacketWriterMutSlice<'a> { +impl<'a, 'b, 'c> std::ops::DerefMut for RtpPacketWriterMutSlice<'a, 'b, 'c> { fn deref_mut(&mut self) -> &mut Self::Target { self.output } } -impl<'a> RtpPacketWriter for RtpPacketWriterMutSlice<'a> { +impl<'a, 'b, 'c> RtpPacketWriter for RtpPacketWriterMutSlice<'a, 'b, 'c> { type Output = usize; - type Payload = &'a [u8]; - type Extension = &'a [u8]; + type Payload = &'b [u8]; + type Extension = &'c [u8]; fn max_size(&self) -> Option { Some(self.output.len()) @@ -503,21 +507,23 @@ impl<'a> RtpPacketWriter for RtpPacketWriterMutSlice<'a> { /// written will be appended to the provide `Vec`. You can `clear()` the vec in between packets /// to have each packet written from the beginning of the vec. #[derive(Debug)] -pub struct RtpPacketWriterMutVec<'a> { +pub struct RtpPacketWriterMutVec<'a, 'b, 'c> { output: &'a mut Vec, padding: Option, + phantom: PhantomData<(&'b [u8], &'c [u8])>, } -impl<'a> RtpPacketWriterMutVec<'a> { +impl<'a, 'b, 'c> RtpPacketWriterMutVec<'a, 'b, 'c> { pub fn new(buf: &'a mut Vec) -> Self { Self { output: buf, padding: None, + phantom: PhantomData, } } } -impl<'a> std::ops::Deref for RtpPacketWriterMutVec<'a> { +impl<'a, 'b, 'c> std::ops::Deref for RtpPacketWriterMutVec<'a, 'b, 'c> { type Target = Vec; fn deref(&self) -> &Self::Target { @@ -525,16 +531,16 @@ impl<'a> std::ops::Deref for RtpPacketWriterMutVec<'a> { } } -impl<'a> std::ops::DerefMut for RtpPacketWriterMutVec<'a> { +impl<'a, 'b, 'c> std::ops::DerefMut for RtpPacketWriterMutVec<'a, 'b, 'c> { fn deref_mut(&mut self) -> &mut Self::Target { self.output } } -impl<'a> RtpPacketWriter for RtpPacketWriterMutVec<'a> { +impl<'a, 'b, 'c> RtpPacketWriter for RtpPacketWriterMutVec<'a, 'b, 'c> { type Output = (); - type Payload = &'a [u8]; - type Extension = &'a [u8]; + type Payload = &'b [u8]; + type Extension = &'c [u8]; fn push(&mut self, data: &[u8]) { self.output.extend(data);