Skip to content

Commit b11388e

Browse files
committed
Refactor and improve code quality across multiple modules
- Updated `store_in_map` methods in `value.rs` to use `clone()` instead of `to_vec()` for better performance. - Simplified lifetime annotations in `errors.rs` for `GetFrameError`. - Enhanced documentation comments in `mod.rs`, `plugin.rs`, and `environment.rs` to include backticks for better readability. - Changed function signatures to `const fn` where applicable for improved compile-time guarantees. - Added `#[must_use]` attributes to several functions to encourage handling of return values. - Improved error handling and type casting in various functions for better safety and clarity. - Removed unnecessary `Deref` and `DerefMut` imports in `environment.rs`. - Updated `ScriptResult` type to be public for better accessibility.
1 parent 64b17b2 commit b11388e

25 files changed

Lines changed: 462 additions & 345 deletions

File tree

rustsynth-sys/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ pub const VSSCRIPT_API_VERSION: i32 =
5353

5454
/// Convenience function for checking if the format never changes between frames
5555
#[inline]
56-
pub unsafe fn isConstantVideoFormat(vi: *const VSVideoInfo) -> i32 {
56+
pub const unsafe fn isConstantVideoFormat(vi: *const VSVideoInfo) -> i32 {
5757
let vi = &*vi;
5858
(vi.height > 0 && vi.width > 0 && vi.format.colorFamily != VSColorFamily::cfUndefined as i32)
5959
as i32
6060
}
6161

6262
/// Convenience function to check if two clips have the same format
6363
#[inline]
64-
pub unsafe fn isSameVideoFormat(v1: *const VSVideoFormat, v2: *const VSVideoFormat) -> i32 {
64+
pub const unsafe fn isSameVideoFormat(v1: *const VSVideoFormat, v2: *const VSVideoFormat) -> i32 {
6565
let v1 = &*v1;
6666
let v2 = &*v2;
6767
(v1.colorFamily == v2.colorFamily
@@ -94,7 +94,7 @@ pub unsafe fn isSameVideoPresetFormat(
9494

9595
/// Convenience function to check if two clips have the same format while also including width and height
9696
#[inline]
97-
pub unsafe fn isSameVideoInfo(v1: *const VSVideoInfo, v2: *const VSVideoInfo) -> i32 {
97+
pub const unsafe fn isSameVideoInfo(v1: *const VSVideoInfo, v2: *const VSVideoInfo) -> i32 {
9898
let v1 = &*v1;
9999
let v2 = &*v2;
100100
(v1.height == v2.height
@@ -104,7 +104,7 @@ pub unsafe fn isSameVideoInfo(v1: *const VSVideoInfo, v2: *const VSVideoInfo) ->
104104

105105
/// Convenience function to check if two clips have the same audio format
106106
#[inline]
107-
pub unsafe fn isSameAudioFormat(a1: *const VSAudioFormat, a2: *const VSAudioFormat) -> i32 {
107+
pub const unsafe fn isSameAudioFormat(a1: *const VSAudioFormat, a2: *const VSAudioFormat) -> i32 {
108108
let a1 = &*a1;
109109
let a2 = &*a2;
110110
(a1.bitsPerSample == a2.bitsPerSample
@@ -114,15 +114,15 @@ pub unsafe fn isSameAudioFormat(a1: *const VSAudioFormat, a2: *const VSAudioForm
114114

115115
/// Convenience function to check if two clips have the same audio info
116116
#[inline]
117-
pub unsafe fn isSameAudioInfo(a1: *const VSAudioInfo, a2: *const VSAudioInfo) -> i32 {
117+
pub const unsafe fn isSameAudioInfo(a1: *const VSAudioInfo, a2: *const VSAudioInfo) -> i32 {
118118
let a1 = &*a1;
119119
let a2 = &*a2;
120120
(a1.sampleRate == a2.sampleRate && isSameAudioFormat(&a1.format, &a2.format) != 0) as i32
121121
}
122122

123123
/// Multiplies and divides a rational number and reduces the result
124124
#[inline]
125-
pub unsafe fn muldivRational(num: *mut i64, den: *mut i64, mul: i64, div: i64) {
125+
pub const unsafe fn muldivRational(num: *mut i64, den: *mut i64, mul: i64, div: i64) {
126126
if *den == 0 {
127127
return;
128128
}
@@ -146,13 +146,13 @@ pub unsafe fn muldivRational(num: *mut i64, den: *mut i64, mul: i64, div: i64) {
146146

147147
/// Reduces a rational number
148148
#[inline]
149-
pub unsafe fn reduceRational(num: *mut i64, den: *mut i64) {
149+
pub const unsafe fn reduceRational(num: *mut i64, den: *mut i64) {
150150
muldivRational(num, den, 1, 1);
151151
}
152152

153153
/// Add two rational numbers and reduces the result
154154
#[inline]
155-
pub unsafe fn addRational(num: *mut i64, den: *mut i64, addnum: i64, addden: i64) {
155+
pub const unsafe fn addRational(num: *mut i64, den: *mut i64, addnum: i64, addden: i64) {
156156
if *den == 0 {
157157
return;
158158
}
@@ -173,7 +173,7 @@ pub unsafe fn addRational(num: *mut i64, den: *mut i64, addnum: i64, addden: i64
173173

174174
/// Converts an int64 to int with saturation
175175
#[inline]
176-
pub fn int64ToIntS(i: i64) -> i32 {
176+
pub const fn int64ToIntS(i: i64) -> i32 {
177177
if i > i32::MAX as i64 {
178178
i32::MAX
179179
} else if i < i32::MIN as i64 {
@@ -185,7 +185,7 @@ pub fn int64ToIntS(i: i64) -> i32 {
185185

186186
/// Converts a double to float with saturation
187187
#[inline]
188-
pub fn doubleToFloatS(d: f64) -> f32 {
188+
pub const fn doubleToFloatS(d: f64) -> f32 {
189189
d as f32
190190
}
191191

@@ -218,7 +218,7 @@ pub unsafe fn bitblt(
218218

219219
/// Check if the frame dimensions are valid for a given format
220220
#[inline]
221-
pub unsafe fn areValidDimensions(fi: *const VSVideoFormat, width: i32, height: i32) -> i32 {
221+
pub const unsafe fn areValidDimensions(fi: *const VSVideoFormat, width: i32, height: i32) -> i32 {
222222
let fi = &*fi;
223223
(width % (1 << fi.subSamplingW) == 0 && height % (1 << fi.subSamplingH) == 0) as i32
224224
}

rustsynth/src/api.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Module for interacting with the VapourSynth API
1+
//! Module for interacting with the `VapourSynth` API
22
use rustsynth_sys as ffi;
33
use std::{
44
borrow::Cow,
@@ -14,7 +14,7 @@ use crate::{
1414

1515
use std::mem::MaybeUninit;
1616

17-
/// A wrapper for the VapourSynth API.
17+
/// A wrapper for the `VapourSynth` API.
1818
///
1919
///
2020
#[derive(Debug, Clone, Copy)]
@@ -62,7 +62,7 @@ macro_rules! map_set_something {
6262
}
6363

6464
impl API {
65-
/// Creates and or retrieves the VapourSynth API.
65+
/// Creates and or retrieves the `VapourSynth` API.
6666
///
6767
/// Returns `None` on error
6868
#[cfg(feature = "vapoursynth-functions")]
@@ -74,7 +74,7 @@ impl API {
7474
let handle = if handle.is_null() {
7575
// Attempt retrieving it otherwise.
7676
let handle =
77-
unsafe { ffi::getVapourSynthAPI(ffi::VAPOURSYNTH_API_VERSION) } as *mut ffi::VSAPI;
77+
unsafe { ffi::getVapourSynthAPI(ffi::VAPOURSYNTH_API_VERSION) }.cast_mut();
7878

7979
if !handle.is_null() {
8080
// If we successfully retrieved the API, cache it.
@@ -115,14 +115,14 @@ impl API {
115115
}
116116

117117
pub(crate) unsafe fn free_core(&self, core: *mut ffi::VSCore) {
118-
self.handle.as_ref().freeCore.unwrap()(core)
118+
self.handle.as_ref().freeCore.unwrap()(core);
119119
}
120120

121121
pub(crate) unsafe fn free_func(&self, function: *mut ffi::VSFunction) {
122-
self.handle.as_ref().freeFunction.unwrap()(function)
122+
self.handle.as_ref().freeFunction.unwrap()(function);
123123
}
124124

125-
pub(crate) fn plugins<'core>(&self, core: &'core CoreRef<'core>) -> Plugins<'core> {
125+
pub(crate) const fn plugins<'core>(&self, core: &'core CoreRef<'core>) -> Plugins<'core> {
126126
Plugins::new(core)
127127
}
128128

@@ -270,7 +270,7 @@ impl API {
270270
}
271271

272272
pub(crate) unsafe fn free_map(&self, map: &mut ffi::VSMap) {
273-
self.handle.as_ref().freeMap.unwrap()(map)
273+
self.handle.as_ref().freeMap.unwrap()(map);
274274
}
275275

276276
pub(crate) unsafe fn map_get_type(&self, map: &ffi::VSMap, key: *const c_char) -> c_int {
@@ -328,7 +328,7 @@ impl API {
328328
}
329329

330330
pub(crate) unsafe fn set_cache_mode(&self, node: *mut ffi::VSNode, mode: i32) {
331-
self.handle.as_ref().setCacheMode.unwrap()(node, mode)
331+
self.handle.as_ref().setCacheMode.unwrap()(node, mode);
332332
}
333333

334334
pub(crate) unsafe fn set_cache_options(
@@ -338,15 +338,15 @@ impl API {
338338
max_size: i32,
339339
max_history_size: i32,
340340
) {
341-
self.handle.as_ref().setCacheOptions.unwrap()(node, fixed_size, max_size, max_history_size)
341+
self.handle.as_ref().setCacheOptions.unwrap()(node, fixed_size, max_size, max_history_size);
342342
}
343343

344344
pub(crate) unsafe fn free_node(&self, node: *mut ffi::VSNode) {
345-
self.handle.as_ref().freeNode.unwrap()(node)
345+
self.handle.as_ref().freeNode.unwrap()(node);
346346
}
347347

348348
pub(crate) unsafe fn free_frame(&self, frame: *const ffi::VSFrame) {
349-
self.handle.as_ref().freeFrame.unwrap()(frame)
349+
self.handle.as_ref().freeFrame.unwrap()(frame);
350350
}
351351

352352
pub(crate) unsafe fn copy_frame(
@@ -387,7 +387,7 @@ impl API {
387387
}
388388

389389
pub(crate) unsafe fn map_set_error(&self, map: &mut ffi::VSMap, error: *const c_char) {
390-
self.handle.as_ref().mapSetError.unwrap()(map, error)
390+
self.handle.as_ref().mapSetError.unwrap()(map, error);
391391
}
392392

393393
pub(crate) unsafe fn set_thread_count(&self, core: *mut ffi::VSCore, count: i32) -> i32 {
@@ -407,13 +407,13 @@ impl API {
407407
append: ffi::VSMapAppendMode,
408408
) -> i32 {
409409
let length = value.len();
410-
assert!(length <= i32::MAX as usize);
410+
assert!(i32::try_from(length).is_ok());
411411
let length = length as i32;
412412

413413
self.handle.as_ref().mapSetData.unwrap()(
414414
map,
415415
key,
416-
value.as_ptr() as _,
416+
value.as_ptr().cast(),
417417
length,
418418
data_type as i32,
419419
append as i32,
@@ -540,7 +540,7 @@ impl API {
540540
err_msg: &mut [c_char],
541541
) -> *const ffi::VSFrame {
542542
let len = err_msg.len();
543-
assert!(len <= i32::MAX as usize);
543+
assert!(i32::try_from(len).is_ok());
544544
let len = len as i32;
545545
self.handle.as_ref().getFrame.unwrap()(n, node, err_msg.as_mut_ptr(), len)
546546
}
@@ -569,7 +569,7 @@ impl API {
569569
>,
570570
user_data: *mut c_void,
571571
) {
572-
self.handle.as_ref().getFrameAsync.unwrap()(n, node, callback, user_data)
572+
self.handle.as_ref().getFrameAsync.unwrap()(n, node, callback, user_data);
573573
}
574574

575575
pub(crate) unsafe fn request_frame_filter(
@@ -578,7 +578,7 @@ impl API {
578578
node: *mut ffi::VSNode,
579579
frame_ctx: *mut ffi::VSFrameContext,
580580
) {
581-
self.handle.as_ref().requestFrameFilter.unwrap()(n, node, frame_ctx)
581+
self.handle.as_ref().requestFrameFilter.unwrap()(n, node, frame_ctx);
582582
}
583583

584584
pub(crate) unsafe fn get_frame_filter(
@@ -618,7 +618,7 @@ impl API {
618618
in_map: &ffi::VSMap,
619619
out_map: &mut ffi::VSMap,
620620
) {
621-
self.handle.as_ref().callFunction.unwrap()(function, in_map, out_map)
621+
self.handle.as_ref().callFunction.unwrap()(function, in_map, out_map);
622622
}
623623

624624
#[allow(clippy::too_many_arguments)]
@@ -1044,15 +1044,15 @@ impl API {
10441044
}
10451045
}
10461046

1047-
/// Initialize the global API pointer (for use in environments where initilising an API is already done(e.g. VapourSynth plugins, script environments).)
1047+
/// Initialize the global API pointer (for use in environments where initilising an API is already done(e.g. `VapourSynth` plugins, script environments).)
10481048
///
10491049
/// It is not necessary to call this function when using the library in a standalone application, as the API will be initialized automatically when creating a Core.
10501050
///
10511051
/// # Safety
1052-
/// This function should only be called once, with a valid pointer to the VapourSynth API
1052+
/// This function should only be called once, with a valid pointer to the `VapourSynth` API
10531053
#[inline]
10541054
pub unsafe fn init_api(vsapi: *const ffi::VSAPI) {
1055-
RAW_API.store(vsapi as *mut ffi::VSAPI, Ordering::Relaxed);
1055+
RAW_API.store(vsapi.cast_mut(), Ordering::Relaxed);
10561056
}
10571057

10581058
#[cfg(test)]

0 commit comments

Comments
 (0)