Use mem::ManuallyDrop instead of mem::forget() everywhere

It makes the intentions clearer and potentially results in simpler
assembly, at least in debug builds.
This commit is contained in:
Sebastian Dröge 2020-04-09 15:59:14 +03:00
parent 973ac7344e
commit 5479b23eb9
11 changed files with 48 additions and 53 deletions

View file

@ -128,9 +128,8 @@ impl Harness {
pub fn add_sink_harness(&mut self, sink_harness: Harness) { pub fn add_sink_harness(&mut self, sink_harness: Harness) {
unsafe { 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()); 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) { pub fn add_src_harness(&mut self, src_harness: Harness, has_clock_wait: bool) {
unsafe { unsafe {
let src_harness = mem::ManuallyDrop::new(src_harness);
gst_check_sys::gst_harness_add_src_harness( gst_check_sys::gst_harness_add_src_harness(
self.0.as_ptr(), self.0.as_ptr(),
src_harness.0.as_ptr(), src_harness.0.as_ptr(),
has_clock_wait.to_glib(), has_clock_wait.to_glib(),
); );
mem::forget(src_harness);
} }
} }

View file

@ -96,9 +96,9 @@ impl PlayerConfig {
} }
} }
pub unsafe fn into_ptr(mut self) -> *mut gst_sys::GstStructure { pub unsafe fn into_ptr(self) -> *mut gst_sys::GstStructure {
let ptr = self.0.to_glib_none_mut().0; let mut s = mem::ManuallyDrop::new(self);
mem::forget(self); let ptr = s.0.to_glib_none_mut().0;
ptr ptr
} }
} }

View file

@ -11,6 +11,8 @@ use gst_rtsp_server_sys;
use glib::subclass::prelude::*; use glib::subclass::prelude::*;
use glib::translate::*; use glib::translate::*;
use std::mem;
use RTSPClient; use RTSPClient;
use RTSPClientClass; use RTSPClientClass;
@ -841,9 +843,8 @@ where
let imp = instance.get_impl(); let imp = instance.get_impl();
let wrap: RTSPClient = from_glib_borrow(ptr); let wrap: RTSPClient = 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; let ptr = sdp.to_glib_none().0;
std::mem::forget(sdp);
ptr as *mut _ ptr as *mut _
} }

View file

@ -386,7 +386,7 @@ impl SDPMediaRef {
} }
} }
pub fn insert_attribute(&mut self, idx: Option<u32>, mut attr: SDPAttribute) -> Result<(), ()> { pub fn insert_attribute(&mut self, idx: Option<u32>, attr: SDPAttribute) -> Result<(), ()> {
if let Some(idx) = idx { if let Some(idx) = idx {
if idx >= self.attributes_len() { if idx >= self.attributes_len() {
return Err(()); return Err(());
@ -394,16 +394,16 @@ impl SDPMediaRef {
} }
let idx = idx.map(|idx| idx as i32).unwrap_or(-1); let idx = idx.map(|idx| idx as i32).unwrap_or(-1);
let mut attr = mem::ManuallyDrop::new(attr);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_media_insert_attribute(&mut self.0, idx, &mut attr.0) }; unsafe { gst_sdp_sys::gst_sdp_media_insert_attribute(&mut self.0, idx, &mut attr.0) };
mem::forget(attr);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => Err(()),
} }
} }
pub fn insert_bandwidth(&mut self, idx: Option<u32>, mut bw: SDPBandwidth) -> Result<(), ()> { pub fn insert_bandwidth(&mut self, idx: Option<u32>, bw: SDPBandwidth) -> Result<(), ()> {
if let Some(idx) = idx { if let Some(idx) = idx {
if idx >= self.bandwidths_len() { if idx >= self.bandwidths_len() {
return Err(()); return Err(());
@ -411,20 +411,16 @@ impl SDPMediaRef {
} }
let idx = idx.map(|idx| idx as i32).unwrap_or(-1); let idx = idx.map(|idx| idx as i32).unwrap_or(-1);
let mut bw = mem::ManuallyDrop::new(bw);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_media_insert_bandwidth(&mut self.0, idx, &mut bw.0) }; unsafe { gst_sdp_sys::gst_sdp_media_insert_bandwidth(&mut self.0, idx, &mut bw.0) };
mem::forget(bw);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => Err(()),
} }
} }
pub fn insert_connection( pub fn insert_connection(&mut self, idx: Option<u32>, conn: SDPConnection) -> Result<(), ()> {
&mut self,
idx: Option<u32>,
mut conn: SDPConnection,
) -> Result<(), ()> {
if let Some(idx) = idx { if let Some(idx) = idx {
if idx >= self.connections_len() { if idx >= self.connections_len() {
return Err(()); return Err(());
@ -432,9 +428,9 @@ impl SDPMediaRef {
} }
let idx = idx.map(|idx| idx as i32).unwrap_or(-1); let idx = idx.map(|idx| idx as i32).unwrap_or(-1);
let mut conn = mem::ManuallyDrop::new(conn);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_media_insert_connection(&mut self.0, idx, &mut conn.0) }; unsafe { gst_sdp_sys::gst_sdp_media_insert_connection(&mut self.0, idx, &mut conn.0) };
mem::forget(conn);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => 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() { if idx >= self.attributes_len() {
return Err(()); return Err(());
} }
let mut attr = mem::ManuallyDrop::new(attr);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_media_replace_attribute(&mut self.0, idx, &mut attr.0) }; unsafe { gst_sdp_sys::gst_sdp_media_replace_attribute(&mut self.0, idx, &mut attr.0) };
mem::forget(attr);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => 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() { if idx >= self.bandwidths_len() {
return Err(()); return Err(());
} }
let mut bw = mem::ManuallyDrop::new(bw);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_media_replace_bandwidth(&mut self.0, idx, &mut bw.0) }; unsafe { gst_sdp_sys::gst_sdp_media_replace_bandwidth(&mut self.0, idx, &mut bw.0) };
mem::forget(bw);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => 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() { if idx >= self.connections_len() {
return Err(()); return Err(());
} }
let mut conn = mem::ManuallyDrop::new(conn);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_media_replace_connection(&mut self.0, idx, &mut conn.0) }; unsafe { gst_sdp_sys::gst_sdp_media_replace_connection(&mut self.0, idx, &mut conn.0) };
mem::forget(conn);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => Err(()),

View file

@ -508,7 +508,7 @@ impl SDPMessageRef {
} }
} }
pub fn insert_attribute(&mut self, idx: Option<u32>, mut attr: SDPAttribute) -> Result<(), ()> { pub fn insert_attribute(&mut self, idx: Option<u32>, attr: SDPAttribute) -> Result<(), ()> {
if let Some(idx) = idx { if let Some(idx) = idx {
if idx >= self.attributes_len() { if idx >= self.attributes_len() {
return Err(()); return Err(());
@ -516,16 +516,16 @@ impl SDPMessageRef {
} }
let idx = idx.map(|idx| idx as i32).unwrap_or(-1); let idx = idx.map(|idx| idx as i32).unwrap_or(-1);
let mut attr = mem::ManuallyDrop::new(attr);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_message_insert_attribute(&mut self.0, idx, &mut attr.0) }; unsafe { gst_sdp_sys::gst_sdp_message_insert_attribute(&mut self.0, idx, &mut attr.0) };
mem::forget(attr);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => Err(()),
} }
} }
pub fn insert_bandwidth(&mut self, idx: Option<u32>, mut bw: SDPBandwidth) -> Result<(), ()> { pub fn insert_bandwidth(&mut self, idx: Option<u32>, bw: SDPBandwidth) -> Result<(), ()> {
if let Some(idx) = idx { if let Some(idx) = idx {
if idx >= self.bandwidths_len() { if idx >= self.bandwidths_len() {
return Err(()); return Err(());
@ -533,9 +533,9 @@ impl SDPMessageRef {
} }
let idx = idx.map(|idx| idx as i32).unwrap_or(-1); let idx = idx.map(|idx| idx as i32).unwrap_or(-1);
let mut bw = mem::ManuallyDrop::new(bw);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_message_insert_bandwidth(&mut self.0, idx, &mut bw.0) }; unsafe { gst_sdp_sys::gst_sdp_message_insert_bandwidth(&mut self.0, idx, &mut bw.0) };
mem::forget(bw);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => Err(()),
@ -576,7 +576,7 @@ impl SDPMessageRef {
} }
} }
pub fn insert_time(&mut self, idx: Option<u32>, mut time: SDPTime) -> Result<(), ()> { pub fn insert_time(&mut self, idx: Option<u32>, time: SDPTime) -> Result<(), ()> {
if let Some(idx) = idx { if let Some(idx) = idx {
if idx >= self.times_len() { if idx >= self.times_len() {
return Err(()); return Err(());
@ -584,16 +584,16 @@ impl SDPMessageRef {
} }
let idx = idx.map(|idx| idx as i32).unwrap_or(-1); let idx = idx.map(|idx| idx as i32).unwrap_or(-1);
let mut time = mem::ManuallyDrop::new(time);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_message_insert_time(&mut self.0, idx, &mut time.0) }; unsafe { gst_sdp_sys::gst_sdp_message_insert_time(&mut self.0, idx, &mut time.0) };
mem::forget(time);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => Err(()),
} }
} }
pub fn insert_zone(&mut self, idx: Option<u32>, mut zone: SDPZone) -> Result<(), ()> { pub fn insert_zone(&mut self, idx: Option<u32>, zone: SDPZone) -> Result<(), ()> {
if let Some(idx) = idx { if let Some(idx) = idx {
if idx >= self.zones_len() { if idx >= self.zones_len() {
return Err(()); return Err(());
@ -601,9 +601,9 @@ impl SDPMessageRef {
} }
let idx = idx.map(|idx| idx as i32).unwrap_or(-1); let idx = idx.map(|idx| idx as i32).unwrap_or(-1);
let mut zone = mem::ManuallyDrop::new(zone);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_message_insert_zone(&mut self.0, idx, &mut zone.0) }; unsafe { gst_sdp_sys::gst_sdp_message_insert_zone(&mut self.0, idx, &mut zone.0) };
mem::forget(zone);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => 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() { if idx >= self.attributes_len() {
return Err(()); return Err(());
} }
let mut attr = mem::ManuallyDrop::new(attr);
let result = unsafe { let result = unsafe {
gst_sdp_sys::gst_sdp_message_replace_attribute(&mut self.0, idx, &mut attr.0) gst_sdp_sys::gst_sdp_message_replace_attribute(&mut self.0, idx, &mut attr.0)
}; };
mem::forget(attr);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => 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() { if idx >= self.bandwidths_len() {
return Err(()); return Err(());
} }
let mut bw = mem::ManuallyDrop::new(bw);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_message_replace_bandwidth(&mut self.0, idx, &mut bw.0) }; unsafe { gst_sdp_sys::gst_sdp_message_replace_bandwidth(&mut self.0, idx, &mut bw.0) };
mem::forget(bw);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => 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() { if idx >= self.times_len() {
return Err(()); return Err(());
} }
let mut time = mem::ManuallyDrop::new(time);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_message_replace_time(&mut self.0, idx, &mut time.0) }; unsafe { gst_sdp_sys::gst_sdp_message_replace_time(&mut self.0, idx, &mut time.0) };
mem::forget(time);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => 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() { if idx >= self.zones_len() {
return Err(()); return Err(());
} }
let mut zone = mem::ManuallyDrop::new(zone);
let result = let result =
unsafe { gst_sdp_sys::gst_sdp_message_replace_zone(&mut self.0, idx, &mut zone.0) }; unsafe { gst_sdp_sys::gst_sdp_message_replace_zone(&mut self.0, idx, &mut zone.0) };
mem::forget(zone);
match result { match result {
gst_sdp_sys::GST_SDP_OK => Ok(()), gst_sdp_sys::GST_SDP_OK => Ok(()),
_ => Err(()), _ => Err(()),

View file

@ -198,8 +198,8 @@ impl<'a> VideoCodecFrame<'a> {
let stream_lock = self.element.get_stream_lock(); let stream_lock = self.element.get_stream_lock();
glib_sys::g_rec_mutex_unlock(stream_lock); glib_sys::g_rec_mutex_unlock(stream_lock);
let ptr = self.to_glib_none().0; let s = mem::ManuallyDrop::new(self);
mem::forget(self); let ptr = s.to_glib_none().0;
ptr ptr
} }

View file

@ -14,14 +14,14 @@ use WebRTCSDPType;
use WebRTCSessionDescription; use WebRTCSessionDescription;
impl 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!(); assert_initialized_main_thread!();
unsafe { unsafe {
let mut sdp = mem::ManuallyDrop::new(sdp);
let desc = from_glib_full(gst_web_rtc_sys::gst_webrtc_session_description_new( let desc = from_glib_full(gst_web_rtc_sys::gst_webrtc_session_description_new(
type_.to_glib(), type_.to_glib(),
sdp.to_glib_none_mut().0, sdp.to_glib_none_mut().0,
)); ));
mem::forget(sdp);
desc desc
} }
} }

View file

@ -64,8 +64,8 @@ impl CapsFeatures {
} }
pub unsafe fn into_ptr(self) -> *mut gst_sys::GstCapsFeatures { pub unsafe fn into_ptr(self) -> *mut gst_sys::GstCapsFeatures {
let ptr = self.0.as_ptr() as *mut CapsFeaturesRef as *mut gst_sys::GstCapsFeatures; let s = mem::ManuallyDrop::new(self);
mem::forget(self); let ptr = s.0.as_ptr() as *mut CapsFeaturesRef as *mut gst_sys::GstCapsFeatures;
ptr ptr
} }

View file

@ -60,8 +60,8 @@ where
for<'a> T: FromValueOptional<'a> + 'static, for<'a> T: FromValueOptional<'a> + 'static,
{ {
pub unsafe fn into_ptr(self) -> *mut gst_sys::GstIterator { pub unsafe fn into_ptr(self) -> *mut gst_sys::GstIterator {
let it = self.to_glib_none().0; let s = mem::ManuallyDrop::new(self);
mem::forget(self); let it = s.to_glib_none().0;
it as *mut _ it as *mut _
} }

View file

@ -94,8 +94,8 @@ impl<T: MiniObject> GstRc<T> {
} }
pub unsafe fn into_ptr(self) -> *mut T::GstType { pub unsafe fn into_ptr(self) -> *mut T::GstType {
let ptr = self.as_mut_ptr(); let s = mem::ManuallyDrop::new(self);
mem::forget(self); let ptr = s.as_mut_ptr();
ptr ptr
} }

View file

@ -110,8 +110,8 @@ impl Structure {
} }
pub unsafe fn into_ptr(self) -> *mut gst_sys::GstStructure { pub unsafe fn into_ptr(self) -> *mut gst_sys::GstStructure {
let ptr = self.0.as_ptr() as *mut StructureRef as *mut gst_sys::GstStructure; let s = mem::ManuallyDrop::new(self);
mem::forget(self); let ptr = s.0.as_ptr() as *mut StructureRef as *mut gst_sys::GstStructure;
ptr ptr
} }