From e63a0a50476b149ae2ec08ab205a362ec1efde9f Mon Sep 17 00:00:00 2001 From: tilpner Date: Tue, 25 Apr 2023 13:57:45 +0200 Subject: [PATCH] fix(codec/packet): clone by referencing packet data --- src/codec/packet/packet.rs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/codec/packet/packet.rs b/src/codec/packet/packet.rs index 0fe65e4..75259f6 100644 --- a/src/codec/packet/packet.rs +++ b/src/codec/packet/packet.rs @@ -228,6 +228,21 @@ impl Packet { } } } + + #[inline] + pub fn try_clone(&self) -> Result { + unsafe { + // This doesn't use av_packet_clone, because it masks + // any av_packet_ref errors by returning a null pointer. + + let mut frame = Packet::empty(); + + match av_packet_ref(frame.as_mut_ptr(), self.as_ptr()) { + 0 => Ok(frame), + e => Err(Error::from(e)), + } + } + } } impl Ref for Packet { @@ -242,23 +257,6 @@ impl Mut for Packet { } } -impl Clone for Packet { - #[inline] - fn clone(&self) -> Self { - let mut pkt = Packet::empty(); - pkt.clone_from(self); - - pkt - } - - #[inline] - fn clone_from(&mut self, source: &Self) { - unsafe { - av_copy_packet(&mut self.0, &source.0); - } - } -} - impl Drop for Packet { fn drop(&mut self) { unsafe {