From 62c0f5ef426d244db2a1a9975756cda6cb122785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 9 Apr 2020 15:59:14 +0300 Subject: [PATCH] Use mem::ManuallyDrop instead of mem::forget() everywhere It makes the intentions clearer and potentially results in simpler assembly, at least in debug builds. --- gstreamer-check/src/harness.rs | 6 ++-- gstreamer-player/src/config.rs | 6 ++-- .../src/subclass/rtsp_client.rs | 5 +-- gstreamer-sdp/src/sdp_media.rs | 28 +++++++--------- gstreamer-sdp/src/sdp_message.rs | 32 +++++++++---------- gstreamer-video/src/video_codec_frame.rs | 4 +-- .../src/web_rtc_session_description.rs | 4 +-- gstreamer/src/caps_features.rs | 4 +-- gstreamer/src/iterator.rs | 4 +-- gstreamer/src/miniobject.rs | 4 +-- gstreamer/src/structure.rs | 4 +-- 11 files changed, 48 insertions(+), 53 deletions(-) diff --git a/gstreamer-check/src/harness.rs b/gstreamer-check/src/harness.rs index f4d2e543c..fe1cc70a4 100644 --- a/gstreamer-check/src/harness.rs +++ b/gstreamer-check/src/harness.rs @@ -128,9 +128,8 @@ impl Harness { pub fn add_sink_harness(&mut self, sink_harness: Harness) { unsafe { + let sink_harness = mem::ManuallyDrop::new(sink_harness); gst_check_sys::gst_harness_add_sink_harness(self.0.as_ptr(), sink_harness.0.as_ptr()); - - mem::forget(sink_harness); } } @@ -152,13 +151,12 @@ impl Harness { pub fn add_src_harness(&mut self, src_harness: Harness, has_clock_wait: bool) { unsafe { + let src_harness = mem::ManuallyDrop::new(src_harness); gst_check_sys::gst_harness_add_src_harness( self.0.as_ptr(), src_harness.0.as_ptr(), has_clock_wait.to_glib(), ); - - mem::forget(src_harness); } } diff --git a/gstreamer-player/src/config.rs b/gstreamer-player/src/config.rs index 445f1be66..ce5e0f2d8 100644 --- a/gstreamer-player/src/config.rs +++ b/gstreamer-player/src/config.rs @@ -96,9 +96,9 @@ impl PlayerConfig { } } - pub unsafe fn into_ptr(mut self) -> *mut gst_sys::GstStructure { - let ptr = self.0.to_glib_none_mut().0; - mem::forget(self); + pub unsafe fn into_ptr(self) -> *mut gst_sys::GstStructure { + let mut s = mem::ManuallyDrop::new(self); + let ptr = s.0.to_glib_none_mut().0; ptr } } diff --git a/gstreamer-rtsp-server/src/subclass/rtsp_client.rs b/gstreamer-rtsp-server/src/subclass/rtsp_client.rs index 568746d9c..536f85b6a 100644 --- a/gstreamer-rtsp-server/src/subclass/rtsp_client.rs +++ b/gstreamer-rtsp-server/src/subclass/rtsp_client.rs @@ -11,6 +11,8 @@ use gst_rtsp_server_sys; use glib::subclass::prelude::*; use glib::translate::*; +use std::mem; + use RTSPClient; use RTSPClientClass; @@ -841,9 +843,8 @@ where let imp = instance.get_impl(); let wrap: Borrowed = from_glib_borrow(ptr); - let sdp = imp.create_sdp(&wrap, &from_glib_borrow(media)); + let sdp = mem::ManuallyDrop::new(imp.create_sdp(&wrap, &from_glib_borrow(media))); let ptr = sdp.to_glib_none().0; - std::mem::forget(sdp); ptr as *mut _ } diff --git a/gstreamer-sdp/src/sdp_media.rs b/gstreamer-sdp/src/sdp_media.rs index 3a08e36b6..e7a3f563d 100644 --- a/gstreamer-sdp/src/sdp_media.rs +++ b/gstreamer-sdp/src/sdp_media.rs @@ -386,7 +386,7 @@ impl SDPMediaRef { } } - pub fn insert_attribute(&mut self, idx: Option, mut attr: SDPAttribute) -> Result<(), ()> { + pub fn insert_attribute(&mut self, idx: Option, attr: SDPAttribute) -> Result<(), ()> { if let Some(idx) = idx { if idx >= self.attributes_len() { return Err(()); @@ -394,16 +394,16 @@ impl SDPMediaRef { } let idx = idx.map(|idx| idx as i32).unwrap_or(-1); + let mut attr = mem::ManuallyDrop::new(attr); let result = unsafe { gst_sdp_sys::gst_sdp_media_insert_attribute(&mut self.0, idx, &mut attr.0) }; - mem::forget(attr); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), } } - pub fn insert_bandwidth(&mut self, idx: Option, mut bw: SDPBandwidth) -> Result<(), ()> { + pub fn insert_bandwidth(&mut self, idx: Option, bw: SDPBandwidth) -> Result<(), ()> { if let Some(idx) = idx { if idx >= self.bandwidths_len() { return Err(()); @@ -411,20 +411,16 @@ impl SDPMediaRef { } let idx = idx.map(|idx| idx as i32).unwrap_or(-1); + let mut bw = mem::ManuallyDrop::new(bw); let result = unsafe { gst_sdp_sys::gst_sdp_media_insert_bandwidth(&mut self.0, idx, &mut bw.0) }; - mem::forget(bw); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), } } - pub fn insert_connection( - &mut self, - idx: Option, - mut conn: SDPConnection, - ) -> Result<(), ()> { + pub fn insert_connection(&mut self, idx: Option, conn: SDPConnection) -> Result<(), ()> { if let Some(idx) = idx { if idx >= self.connections_len() { return Err(()); @@ -432,9 +428,9 @@ impl SDPMediaRef { } let idx = idx.map(|idx| idx as i32).unwrap_or(-1); + let mut conn = mem::ManuallyDrop::new(conn); let result = unsafe { gst_sdp_sys::gst_sdp_media_insert_connection(&mut self.0, idx, &mut conn.0) }; - mem::forget(conn); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), @@ -506,42 +502,42 @@ impl SDPMediaRef { } } - pub fn replace_attribute(&mut self, idx: u32, mut attr: SDPAttribute) -> Result<(), ()> { + pub fn replace_attribute(&mut self, idx: u32, attr: SDPAttribute) -> Result<(), ()> { if idx >= self.attributes_len() { return Err(()); } + let mut attr = mem::ManuallyDrop::new(attr); let result = unsafe { gst_sdp_sys::gst_sdp_media_replace_attribute(&mut self.0, idx, &mut attr.0) }; - mem::forget(attr); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), } } - pub fn replace_bandwidth(&mut self, idx: u32, mut bw: SDPBandwidth) -> Result<(), ()> { + pub fn replace_bandwidth(&mut self, idx: u32, bw: SDPBandwidth) -> Result<(), ()> { if idx >= self.bandwidths_len() { return Err(()); } + let mut bw = mem::ManuallyDrop::new(bw); let result = unsafe { gst_sdp_sys::gst_sdp_media_replace_bandwidth(&mut self.0, idx, &mut bw.0) }; - mem::forget(bw); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), } } - pub fn replace_connection(&mut self, idx: u32, mut conn: SDPConnection) -> Result<(), ()> { + pub fn replace_connection(&mut self, idx: u32, conn: SDPConnection) -> Result<(), ()> { if idx >= self.connections_len() { return Err(()); } + let mut conn = mem::ManuallyDrop::new(conn); let result = unsafe { gst_sdp_sys::gst_sdp_media_replace_connection(&mut self.0, idx, &mut conn.0) }; - mem::forget(conn); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), diff --git a/gstreamer-sdp/src/sdp_message.rs b/gstreamer-sdp/src/sdp_message.rs index 5064d8072..8720229ef 100644 --- a/gstreamer-sdp/src/sdp_message.rs +++ b/gstreamer-sdp/src/sdp_message.rs @@ -508,7 +508,7 @@ impl SDPMessageRef { } } - pub fn insert_attribute(&mut self, idx: Option, mut attr: SDPAttribute) -> Result<(), ()> { + pub fn insert_attribute(&mut self, idx: Option, attr: SDPAttribute) -> Result<(), ()> { if let Some(idx) = idx { if idx >= self.attributes_len() { return Err(()); @@ -516,16 +516,16 @@ impl SDPMessageRef { } let idx = idx.map(|idx| idx as i32).unwrap_or(-1); + let mut attr = mem::ManuallyDrop::new(attr); let result = unsafe { gst_sdp_sys::gst_sdp_message_insert_attribute(&mut self.0, idx, &mut attr.0) }; - mem::forget(attr); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), } } - pub fn insert_bandwidth(&mut self, idx: Option, mut bw: SDPBandwidth) -> Result<(), ()> { + pub fn insert_bandwidth(&mut self, idx: Option, bw: SDPBandwidth) -> Result<(), ()> { if let Some(idx) = idx { if idx >= self.bandwidths_len() { return Err(()); @@ -533,9 +533,9 @@ impl SDPMessageRef { } let idx = idx.map(|idx| idx as i32).unwrap_or(-1); + let mut bw = mem::ManuallyDrop::new(bw); let result = unsafe { gst_sdp_sys::gst_sdp_message_insert_bandwidth(&mut self.0, idx, &mut bw.0) }; - mem::forget(bw); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), @@ -576,7 +576,7 @@ impl SDPMessageRef { } } - pub fn insert_time(&mut self, idx: Option, mut time: SDPTime) -> Result<(), ()> { + pub fn insert_time(&mut self, idx: Option, time: SDPTime) -> Result<(), ()> { if let Some(idx) = idx { if idx >= self.times_len() { return Err(()); @@ -584,16 +584,16 @@ impl SDPMessageRef { } let idx = idx.map(|idx| idx as i32).unwrap_or(-1); + let mut time = mem::ManuallyDrop::new(time); let result = unsafe { gst_sdp_sys::gst_sdp_message_insert_time(&mut self.0, idx, &mut time.0) }; - mem::forget(time); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), } } - pub fn insert_zone(&mut self, idx: Option, mut zone: SDPZone) -> Result<(), ()> { + pub fn insert_zone(&mut self, idx: Option, zone: SDPZone) -> Result<(), ()> { if let Some(idx) = idx { if idx >= self.zones_len() { return Err(()); @@ -601,9 +601,9 @@ impl SDPMessageRef { } let idx = idx.map(|idx| idx as i32).unwrap_or(-1); + let mut zone = mem::ManuallyDrop::new(zone); let result = unsafe { gst_sdp_sys::gst_sdp_message_insert_zone(&mut self.0, idx, &mut zone.0) }; - mem::forget(zone); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), @@ -690,29 +690,29 @@ impl SDPMessageRef { } } - pub fn replace_attribute(&mut self, idx: u32, mut attr: SDPAttribute) -> Result<(), ()> { + pub fn replace_attribute(&mut self, idx: u32, attr: SDPAttribute) -> Result<(), ()> { if idx >= self.attributes_len() { return Err(()); } + let mut attr = mem::ManuallyDrop::new(attr); let result = unsafe { gst_sdp_sys::gst_sdp_message_replace_attribute(&mut self.0, idx, &mut attr.0) }; - mem::forget(attr); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), } } - pub fn replace_bandwidth(&mut self, idx: u32, mut bw: SDPBandwidth) -> Result<(), ()> { + pub fn replace_bandwidth(&mut self, idx: u32, bw: SDPBandwidth) -> Result<(), ()> { if idx >= self.bandwidths_len() { return Err(()); } + let mut bw = mem::ManuallyDrop::new(bw); let result = unsafe { gst_sdp_sys::gst_sdp_message_replace_bandwidth(&mut self.0, idx, &mut bw.0) }; - mem::forget(bw); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), @@ -747,28 +747,28 @@ impl SDPMessageRef { } } - pub fn replace_time(&mut self, idx: u32, mut time: SDPTime) -> Result<(), ()> { + pub fn replace_time(&mut self, idx: u32, time: SDPTime) -> Result<(), ()> { if idx >= self.times_len() { return Err(()); } + let mut time = mem::ManuallyDrop::new(time); let result = unsafe { gst_sdp_sys::gst_sdp_message_replace_time(&mut self.0, idx, &mut time.0) }; - mem::forget(time); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), } } - pub fn replace_zone(&mut self, idx: u32, mut zone: SDPZone) -> Result<(), ()> { + pub fn replace_zone(&mut self, idx: u32, zone: SDPZone) -> Result<(), ()> { if idx >= self.zones_len() { return Err(()); } + let mut zone = mem::ManuallyDrop::new(zone); let result = unsafe { gst_sdp_sys::gst_sdp_message_replace_zone(&mut self.0, idx, &mut zone.0) }; - mem::forget(zone); match result { gst_sdp_sys::GST_SDP_OK => Ok(()), _ => Err(()), diff --git a/gstreamer-video/src/video_codec_frame.rs b/gstreamer-video/src/video_codec_frame.rs index 109981e9b..1a9a2f1c1 100644 --- a/gstreamer-video/src/video_codec_frame.rs +++ b/gstreamer-video/src/video_codec_frame.rs @@ -198,8 +198,8 @@ impl<'a> VideoCodecFrame<'a> { let stream_lock = self.element.get_stream_lock(); glib_sys::g_rec_mutex_unlock(stream_lock); - let ptr = self.to_glib_none().0; - mem::forget(self); + let s = mem::ManuallyDrop::new(self); + let ptr = s.to_glib_none().0; ptr } diff --git a/gstreamer-webrtc/src/web_rtc_session_description.rs b/gstreamer-webrtc/src/web_rtc_session_description.rs index 736555757..78861c69a 100644 --- a/gstreamer-webrtc/src/web_rtc_session_description.rs +++ b/gstreamer-webrtc/src/web_rtc_session_description.rs @@ -14,14 +14,14 @@ use WebRTCSDPType; use WebRTCSessionDescription; impl WebRTCSessionDescription { - pub fn new(type_: WebRTCSDPType, mut sdp: gst_sdp::SDPMessage) -> WebRTCSessionDescription { + pub fn new(type_: WebRTCSDPType, sdp: gst_sdp::SDPMessage) -> WebRTCSessionDescription { assert_initialized_main_thread!(); unsafe { + let mut sdp = mem::ManuallyDrop::new(sdp); let desc = from_glib_full(gst_web_rtc_sys::gst_webrtc_session_description_new( type_.to_glib(), sdp.to_glib_none_mut().0, )); - mem::forget(sdp); desc } } diff --git a/gstreamer/src/caps_features.rs b/gstreamer/src/caps_features.rs index 4f16a8ac7..0d1a25d24 100644 --- a/gstreamer/src/caps_features.rs +++ b/gstreamer/src/caps_features.rs @@ -66,8 +66,8 @@ impl CapsFeatures { } pub unsafe fn into_ptr(self) -> *mut gst_sys::GstCapsFeatures { - let ptr = self.0.as_ptr() as *mut CapsFeaturesRef as *mut gst_sys::GstCapsFeatures; - mem::forget(self); + let s = mem::ManuallyDrop::new(self); + let ptr = s.0.as_ptr() as *mut CapsFeaturesRef as *mut gst_sys::GstCapsFeatures; ptr } diff --git a/gstreamer/src/iterator.rs b/gstreamer/src/iterator.rs index f70b7ffb9..dde226488 100644 --- a/gstreamer/src/iterator.rs +++ b/gstreamer/src/iterator.rs @@ -43,8 +43,8 @@ where for<'a> T: FromValueOptional<'a> + 'static, { pub unsafe fn into_ptr(self) -> *mut gst_sys::GstIterator { - let it = self.to_glib_none().0; - mem::forget(self); + let s = mem::ManuallyDrop::new(self); + let it = s.to_glib_none().0; it as *mut _ } diff --git a/gstreamer/src/miniobject.rs b/gstreamer/src/miniobject.rs index c045aa388..18a8162fc 100644 --- a/gstreamer/src/miniobject.rs +++ b/gstreamer/src/miniobject.rs @@ -91,8 +91,8 @@ impl GstRc { } pub unsafe fn into_ptr(self) -> *mut T::GstType { - let ptr = self.as_mut_ptr(); - mem::forget(self); + let s = mem::ManuallyDrop::new(self); + let ptr = s.as_mut_ptr(); ptr } diff --git a/gstreamer/src/structure.rs b/gstreamer/src/structure.rs index fdf2d26dc..2a0b5f861 100644 --- a/gstreamer/src/structure.rs +++ b/gstreamer/src/structure.rs @@ -110,8 +110,8 @@ impl Structure { } pub unsafe fn into_ptr(self) -> *mut gst_sys::GstStructure { - let ptr = self.0.as_ptr() as *mut StructureRef as *mut gst_sys::GstStructure; - mem::forget(self); + let s = mem::ManuallyDrop::new(self); + let ptr = s.0.as_ptr() as *mut StructureRef as *mut gst_sys::GstStructure; ptr }