Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit d608f3e

Browse files
authoredMay 10, 2020
Fix Clippy issues (#127)
* Remove unnecessary parentheses * Remove deprecated `Error::description` (closes #123) * Avoid zero initialization of `AEffect` * Use `to_bits` to get bits from `f32`
1 parent f943b56 commit d608f3e

File tree

5 files changed

+83
-78
lines changed

5 files changed

+83
-78
lines changed
 

‎examples/simple_host.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
extern crate vst;
22

33
use std::env;
4-
use std::error::Error;
54
use std::path::Path;
65
use std::process;
76
use std::sync::{Arc, Mutex};
@@ -33,8 +32,8 @@ fn main() {
3332
println!("Loading {}...", path.to_str().unwrap());
3433

3534
// Load the plugin
36-
let mut loader = PluginLoader::load(path, Arc::clone(&host))
37-
.unwrap_or_else(|e| panic!("Failed to load plugin: {}", e.description()));
35+
let mut loader =
36+
PluginLoader::load(path, Arc::clone(&host)).unwrap_or_else(|e| panic!("Failed to load plugin: {}", e));
3837

3938
// Create an instance of the plugin
4039
let mut instance = loader.instance().unwrap();

‎src/host.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,19 @@ pub enum PluginLoadError {
245245

246246
impl fmt::Display for PluginLoadError {
247247
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
248-
write!(f, "{}", self.description())
249-
}
250-
}
251-
252-
impl Error for PluginLoadError {
253-
fn description(&self) -> &str {
254248
use self::PluginLoadError::*;
255-
256-
match *self {
249+
let description = match self {
257250
InvalidPath => "Could not open the requested path",
258251
NotAPlugin => "The given path does not contain a VST2.4 compatible library",
259252
InstanceFailed => "Failed to create a plugin instance",
260253
InvalidApiVersion => "The plugin API version is not compatible with this library",
261-
}
254+
};
255+
write!(f, "{}", description)
262256
}
263257
}
264258

259+
impl Error for PluginLoadError {}
260+
265261
/// Wrapper for an externally loaded VST plugin.
266262
///
267263
/// The only functionality this struct provides is loading plugins, which can be done via the

‎src/interfaces.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub fn dispatch(effect: *mut AEffect, opcode: i32, index: i32, value: isize, ptr
241241
editor.key_down(KeyCode {
242242
character: index as u8 as char,
243243
key: Key::from(value),
244-
modifier: unsafe { mem::transmute::<f32, i32>(opt) } as u8,
244+
modifier: opt.to_bits() as u8,
245245
});
246246
}
247247
}
@@ -250,7 +250,7 @@ pub fn dispatch(effect: *mut AEffect, opcode: i32, index: i32, value: isize, ptr
250250
editor.key_up(KeyCode {
251251
character: index as u8 as char,
252252
key: Key::from(value),
253-
modifier: unsafe { mem::transmute::<f32, i32>(opt) } as u8,
253+
modifier: opt.to_bits() as u8,
254254
});
255255
}
256256
}

‎src/lib.rs

+73-63
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ extern crate log;
113113
#[macro_use]
114114
extern crate bitflags;
115115

116-
use std::{mem, ptr};
116+
use std::ptr;
117117

118118
/// Implements `From` and `Into` for enums with `#[repr(usize)]`. Useful for interfacing with C
119119
/// enums.
@@ -188,88 +188,98 @@ macro_rules! plugin_main {
188188
/// Initializes a VST plugin and returns a raw pointer to an AEffect struct.
189189
#[doc(hidden)]
190190
pub fn main<T: Plugin + Default>(callback: HostCallbackProc) -> *mut AEffect {
191-
// Create a Box containing a zeroed AEffect. This is transmuted into a *mut pointer so that it
192-
// can be passed into the HostCallback `wrap` method. The AEffect is then updated after the vst
193-
// object is created so that the host still contains a raw pointer to the AEffect struct.
194-
let effect = unsafe { Box::into_raw(Box::new(mem::MaybeUninit::zeroed().assume_init())) };
191+
// Initialize as much of the AEffect as we can before creating the plugin.
192+
// In particular, initialize all the function pointers, since initializing
193+
// these to zero is undefined behavior.
194+
let boxed_effect = Box::new(AEffect {
195+
magic: VST_MAGIC,
196+
dispatcher: interfaces::dispatch, // fn pointer
195197

196-
let host = HostCallback::wrap(callback, effect);
197-
if host.vst_version() == 0 {
198-
// TODO: Better criteria would probably be useful here...
199-
return ptr::null_mut();
200-
}
198+
_process: interfaces::process_deprecated, // fn pointer
201199

202-
trace!("Creating VST plugin instance...");
203-
let mut plugin = T::new(host);
204-
let info = plugin.get_info();
205-
let params = plugin.get_parameter_object();
206-
let editor = plugin.get_editor();
200+
setParameter: interfaces::set_parameter, // fn pointer
201+
getParameter: interfaces::get_parameter, // fn pointer
207202

208-
// Update AEffect in place
209-
unsafe {
210-
*effect = AEffect {
211-
magic: VST_MAGIC,
212-
dispatcher: interfaces::dispatch, // fn pointer
213-
214-
_process: interfaces::process_deprecated, // fn pointer
215-
216-
setParameter: interfaces::set_parameter, // fn pointer
217-
getParameter: interfaces::get_parameter, // fn pointer
218-
219-
numPrograms: info.presets,
220-
numParams: info.parameters,
221-
numInputs: info.inputs,
222-
numOutputs: info.outputs,
203+
numPrograms: 0, // To be updated with plugin specific value.
204+
numParams: 0, // To be updated with plugin specific value.
205+
numInputs: 0, // To be updated with plugin specific value.
206+
numOutputs: 0, // To be updated with plugin specific value.
223207

224-
flags: {
225-
use api::PluginFlags;
208+
flags: 0, // To be updated with plugin specific value.
226209

227-
let mut flag = PluginFlags::CAN_REPLACING;
210+
reserved1: 0,
211+
reserved2: 0,
228212

229-
if info.f64_precision {
230-
flag |= PluginFlags::CAN_DOUBLE_REPLACING;
231-
}
213+
initialDelay: 0, // To be updated with plugin specific value.
232214

233-
if editor.is_some() {
234-
flag |= PluginFlags::HAS_EDITOR;
235-
}
215+
_realQualities: 0,
216+
_offQualities: 0,
217+
_ioRatio: 0.0,
236218

237-
if info.preset_chunks {
238-
flag |= PluginFlags::PROGRAM_CHUNKS;
239-
}
219+
object: ptr::null_mut(),
220+
user: ptr::null_mut(),
240221

241-
if let plugin::Category::Synth = info.category {
242-
flag |= PluginFlags::IS_SYNTH;
243-
}
222+
uniqueId: 0, // To be updated with plugin specific value.
223+
version: 0, // To be updated with plugin specific value.
244224

245-
if info.silent_when_stopped {
246-
flag |= PluginFlags::NO_SOUND_IN_STOP;
247-
}
225+
processReplacing: interfaces::process_replacing, // fn pointer
226+
processReplacingF64: interfaces::process_replacing_f64, //fn pointer
248227

249-
flag.bits()
250-
},
228+
future: [0u8; 56],
229+
});
230+
let raw_effect = Box::into_raw(boxed_effect);
251231

252-
reserved1: 0,
253-
reserved2: 0,
232+
let host = HostCallback::wrap(callback, raw_effect);
233+
if host.vst_version() == 0 {
234+
// TODO: Better criteria would probably be useful here...
235+
return ptr::null_mut();
236+
}
254237

255-
initialDelay: info.initial_delay,
238+
trace!("Creating VST plugin instance...");
239+
let mut plugin = T::new(host);
240+
let info = plugin.get_info();
241+
let params = plugin.get_parameter_object();
242+
let editor = plugin.get_editor();
256243

257-
_realQualities: 0,
258-
_offQualities: 0,
259-
_ioRatio: 0.0,
244+
// Update AEffect in place
245+
let effect = unsafe { &mut *raw_effect };
246+
effect.numPrograms = info.presets;
247+
effect.numParams = info.parameters;
248+
effect.numInputs = info.inputs;
249+
effect.numOutputs = info.outputs;
250+
effect.flags = {
251+
use api::PluginFlags;
252+
253+
let mut flag = PluginFlags::CAN_REPLACING;
254+
255+
if info.f64_precision {
256+
flag |= PluginFlags::CAN_DOUBLE_REPLACING;
257+
}
260258

261-
object: Box::into_raw(Box::new(Box::new(plugin) as Box<dyn Plugin>)) as *mut _,
262-
user: Box::into_raw(Box::new(PluginCache::new(&info, params, editor))) as *mut _,
259+
if editor.is_some() {
260+
flag |= PluginFlags::HAS_EDITOR;
261+
}
263262

264-
uniqueId: info.unique_id,
265-
version: info.version,
263+
if info.preset_chunks {
264+
flag |= PluginFlags::PROGRAM_CHUNKS;
265+
}
266266

267-
processReplacing: interfaces::process_replacing, // fn pointer
268-
processReplacingF64: interfaces::process_replacing_f64, //fn pointer
267+
if let plugin::Category::Synth = info.category {
268+
flag |= PluginFlags::IS_SYNTH;
269+
}
269270

270-
future: [0u8; 56],
271+
if info.silent_when_stopped {
272+
flag |= PluginFlags::NO_SOUND_IN_STOP;
271273
}
274+
275+
flag.bits()
272276
};
277+
effect.initialDelay = info.initial_delay;
278+
effect.object = Box::into_raw(Box::new(Box::new(plugin) as Box<dyn Plugin>)) as *mut _;
279+
effect.user = Box::into_raw(Box::new(PluginCache::new(&info, params, editor))) as *mut _;
280+
effect.uniqueId = info.unique_id;
281+
effect.version = info.version;
282+
273283
effect
274284
}
275285

‎src/plugin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ impl Host for HostCallback {
957957

958958
match ptr {
959959
0 => None,
960-
ptr => Some(unsafe { (*(ptr as *const TimeInfo)) }),
960+
ptr => Some(unsafe { *(ptr as *const TimeInfo) }),
961961
}
962962
}
963963

0 commit comments

Comments
 (0)
This repository has been archived.