Skip to content

Commit f847b6e

Browse files
committed
Make "single-use" callbacks FnOnce
This is more general than `FnMut`, which is useful in some cases. jnqnfe#50
1 parent 2f5ae7c commit f847b6e

File tree

9 files changed

+211
-211
lines changed

9 files changed

+211
-211
lines changed

pulse-binding/src/context/ext_device_manager.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,29 +170,29 @@ impl DeviceManager {
170170
///
171171
/// Panics if the underlying C function returns a null pointer.
172172
pub fn set_device_description<F>(&mut self, device: &str, description: &str, callback: F)
173-
-> Operation<dyn FnMut(bool)>
174-
where F: FnMut(bool) + 'static
173+
-> Operation<dyn FnOnce(bool)>
174+
where F: FnOnce(bool) + 'static
175175
{
176176
// Warning: New CStrings will be immediately freed if not bound to a
177177
// variable, leading to as_ptr() giving dangling pointers!
178178
let c_dev = CString::new(device.clone()).unwrap();
179179
let c_desc = CString::new(description.clone()).unwrap();
180180

181-
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
181+
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
182182
let ptr = unsafe {
183183
capi::pa_ext_device_manager_set_device_description(self.context, c_dev.as_ptr(),
184184
c_desc.as_ptr(), Some(super::success_cb_proxy), cb_data)
185185
};
186-
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
186+
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
187187
}
188188

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

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

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

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

260-
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
260+
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
261261
let ptr = unsafe {
262262
capi::pa_ext_device_manager_reorder_devices_for_role(self.context, c_role.as_ptr(),
263263
c_dev_ptrs.as_ptr(), Some(super::success_cb_proxy), cb_data)
264264
};
265-
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
265+
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
266266
}
267267

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

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

pulse-binding/src/context/ext_device_restore.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ impl DeviceRestore {
112112
/// The callback must accept a `bool`, which indicates success.
113113
///
114114
/// Panics if the underlying C function returns a null pointer.
115-
pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnMut(bool)>
116-
where F: FnMut(bool) + 'static
115+
pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnOnce(bool)>
116+
where F: FnOnce(bool) + 'static
117117
{
118-
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
118+
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
119119
let ptr = unsafe { capi::pa_ext_device_restore_subscribe(self.context, enable as i32,
120120
Some(super::success_cb_proxy), cb_data) };
121-
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
121+
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
122122
}
123123

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

180-
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
180+
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
181181
let ptr = unsafe {
182182
capi::pa_ext_device_restore_save_formats(self.context, type_, index,
183183
format_ptrs.len() as u8, format_ptrs.as_ptr(), Some(super::success_cb_proxy),
184184
cb_data)
185185
};
186-
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
186+
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
187187
}
188188
}
189189

pulse-binding/src/context/ext_stream_restore.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,25 @@ impl StreamRestore {
132132
///
133133
/// Panics if the underlying C function returns a null pointer.
134134
pub fn write<F>(&mut self, mode: proplist::UpdateMode, data: &[&Info],
135-
apply_immediately: bool, callback: F) -> Operation<dyn FnMut(bool)>
136-
where F: FnMut(bool) + 'static
135+
apply_immediately: bool, callback: F) -> Operation<dyn FnOnce(bool)>
136+
where F: FnOnce(bool) + 'static
137137
{
138-
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
138+
let cb_data = box_closure_get_capi_ptr::<dyn FnOnce(bool)>(Box::new(callback));
139139
let ptr = unsafe {
140140
capi::pa_ext_stream_restore_write(self.context, mode, mem::transmute(data.as_ptr()),
141141
data.len() as u32, apply_immediately as i32, Some(super::success_cb_proxy),
142142
cb_data)
143143
};
144-
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
144+
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnOnce(bool)>)
145145
}
146146

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

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

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

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

0 commit comments

Comments
 (0)