Skip to content

Commit

Permalink
Make "single-use" callbacks FnOnce
Browse files Browse the repository at this point in the history
This is more general than `FnMut`, which is useful in some cases.

jnqnfe#50
  • Loading branch information
ids1024 committed Jun 23, 2022
1 parent 2f5ae7c commit f847b6e
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 211 deletions.
38 changes: 19 additions & 19 deletions pulse-binding/src/context/ext_device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,29 +170,29 @@ impl DeviceManager {
///
/// Panics if the underlying C function returns a null pointer.
pub fn set_device_description<F>(&mut self, device: &str, description: &str, callback: F)
-> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
-> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
// Warning: New CStrings will be immediately freed if not bound to a
// variable, leading to as_ptr() giving dangling pointers!
let c_dev = CString::new(device.clone()).unwrap();
let c_desc = CString::new(description.clone()).unwrap();

let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe {
capi::pa_ext_device_manager_set_device_description(self.context, c_dev.as_ptr(),
c_desc.as_ptr(), Some(super::success_cb_proxy), cb_data)
};
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Deletes entries from the device database.
///
/// The callback must accept a `bool`, which indicates success.
///
/// Panics if the underlying C function returns a null pointer.
pub fn delete<F>(&mut self, devices: &[&str], callback: F) -> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
pub fn delete<F>(&mut self, devices: &[&str], callback: F) -> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
// Warning: New CStrings will be immediately freed if not bound to a variable, leading to
// as_ptr() giving dangling pointers!
Expand All @@ -209,10 +209,10 @@ impl DeviceManager {
}
c_dev_ptrs.push(null());

let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe { capi::pa_ext_device_manager_delete(self.context, c_dev_ptrs.as_ptr(),
Some(super::success_cb_proxy), cb_data) };
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Enables the role-based device-priority routing mode.
Expand All @@ -221,15 +221,15 @@ impl DeviceManager {
///
/// Panics if the underlying C function returns a null pointer.
pub fn enable_role_device_priority_routing<F>(&mut self, enable: bool, callback: F)
-> Operation<dyn FnMut(bool)>
-> Operation<dyn FnOnce(bool)>
where F: FnMut(bool) + 'static
{
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe {
capi::pa_ext_device_manager_enable_role_device_priority_routing(self.context,
enable as i32, Some(super::success_cb_proxy), cb_data)
};
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Reorders the position of a given device in the priority list to give preference to it.
Expand All @@ -238,8 +238,8 @@ impl DeviceManager {
///
/// Panics if the underlying C function returns a null pointer.
pub fn reorder_devices_for_role<F>(&mut self, role: &str, devices: &[&str], callback: F)
-> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
-> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
// Warning: New CStrings will be immediately freed if not bound to a variable, leading to
// as_ptr() giving dangling pointers!
Expand All @@ -257,26 +257,26 @@ impl DeviceManager {
}
c_dev_ptrs.push(null());

let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe {
capi::pa_ext_device_manager_reorder_devices_for_role(self.context, c_role.as_ptr(),
c_dev_ptrs.as_ptr(), Some(super::success_cb_proxy), cb_data)
};
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Subscribes to changes in the device database.
///
/// The callback must accept a `bool`, which indicates success.
///
/// Panics if the underlying C function returns a null pointer.
pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe { capi::pa_ext_device_manager_subscribe(self.context, enable as i32,
Some(super::success_cb_proxy), cb_data) };
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Sets the subscription callback that is called when [`subscribe()`](Self::subscribe) was
Expand Down
16 changes: 8 additions & 8 deletions pulse-binding/src/context/ext_device_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ impl DeviceRestore {
/// The callback must accept a `bool`, which indicates success.
///
/// Panics if the underlying C function returns a null pointer.
pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe { capi::pa_ext_device_restore_subscribe(self.context, enable as i32,
Some(super::success_cb_proxy), cb_data) };
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Sets the subscription callback that is called when [`subscribe()`](Self::subscribe) was
Expand Down Expand Up @@ -168,22 +168,22 @@ impl DeviceRestore {
///
/// Panics if the underlying C function returns a null pointer.
pub fn save_formats<F>(&mut self, type_: def::Device, index: u32,
formats: &mut [&mut format::Info], callback: F) -> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
formats: &mut [&mut format::Info], callback: F) -> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
// Capture array of pointers to the above `format::InfoInternal` objects
let mut format_ptrs: Vec<*mut capi::pa_format_info> = Vec::with_capacity(formats.len());
for format in formats {
format_ptrs.push(unsafe { mem::transmute(&format.ptr) });
}

let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe {
capi::pa_ext_device_restore_save_formats(self.context, type_, index,
format_ptrs.len() as u8, format_ptrs.as_ptr(), Some(super::success_cb_proxy),
cb_data)
};
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}
}

Expand Down
24 changes: 12 additions & 12 deletions pulse-binding/src/context/ext_stream_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,25 @@ impl StreamRestore {
///
/// Panics if the underlying C function returns a null pointer.
pub fn write<F>(&mut self, mode: proplist::UpdateMode, data: &[&Info],
apply_immediately: bool, callback: F) -> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
apply_immediately: bool, callback: F) -> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe {
capi::pa_ext_stream_restore_write(self.context, mode, mem::transmute(data.as_ptr()),
data.len() as u32, apply_immediately as i32, Some(super::success_cb_proxy),
cb_data)
};
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Deletes entries from the stream database.
///
/// The callback must accept a `bool`, which indicates success.
///
/// Panics if the underlying C function returns a null pointer.
pub fn delete<F>(&mut self, streams: &[&str], callback: F) -> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
pub fn delete<F>(&mut self, streams: &[&str], callback: F) -> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
// Warning: New CStrings will be immediately freed if not bound to a variable, leading to
// as_ptr() giving dangling pointers!
Expand All @@ -167,24 +167,24 @@ impl StreamRestore {
}
c_stream_ptrs.push(null());

let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe { capi::pa_ext_stream_restore_delete(self.context, c_stream_ptrs.as_ptr(),
Some(super::success_cb_proxy), cb_data) };
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Subscribes to changes in the stream database.
///
/// The callback must accept a `bool`, which indicates success.
///
/// Panics if the underlying C function returns a null pointer.
pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnOnce(bool)>
where F: FnOnce(bool) + 'static
{
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
let ptr = unsafe { capi::pa_ext_stream_restore_subscribe(self.context, enable as i32,
Some(super::success_cb_proxy), cb_data) };
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
}

/// Sets the subscription callback that is called when [`subscribe()`](Self::subscribe) was
Expand Down
Loading

0 comments on commit f847b6e

Please sign in to comment.