Skip to content

Commit 911cf3c

Browse files
committed
Refactor map handling: Replace OwnedMap with Map
- Updated instances of OwnedMap to use Map in various modules including main.rs, core.rs, and function.rs. - Adjusted function signatures and return types to reflect the change from OwnedMap to Map. - Modified tests to accommodate the new Map structure instead of OwnedMap. - Removed unused references and definitions related to OwnedMap. - Enhanced data handling in the map module to improve safety and performance.
1 parent fb02f45 commit 911cf3c

File tree

13 files changed

+92
-208
lines changed

13 files changed

+92
-208
lines changed

rspipe/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clap::{Arg, ArgAction, Command};
22
use rustsynth::{
33
core::{CoreCreationFlags, CoreRef},
4-
map::OwnedMap,
4+
map::Map,
55
vsscript::Environment,
66
};
77
use std::collections::HashMap;
@@ -138,7 +138,7 @@ fn main() {
138138
}
139139
}
140140

141-
let mut vars_map = OwnedMap::new();
141+
let mut vars_map = Map::new();
142142
for (key, value) in script_args {
143143
if let Err(e) = vars_map.set(&key, &value) {
144144
eprintln!("Failed to set script variable {}: {}", key, e);

rustsynth/src/api.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Module for interacting with the VapourSynth API
22
use rustsynth_sys as ffi;
33
use std::{
4+
borrow::Cow,
45
ffi::{c_char, c_int, c_void},
56
ptr::{self, NonNull},
67
sync::atomic::{AtomicPtr, Ordering},
@@ -738,7 +739,7 @@ impl API {
738739
pub(crate) fn get_audio_format_name(
739740
&self,
740741
format: *const ffi::VSAudioFormat,
741-
) -> Option<String> {
742+
) -> Option<Cow<'_, str>> {
742743
let mut buf = [0i8; 32]; // Buffer for up to 32 characters as per API docs
743744
let result =
744745
unsafe { self.handle.as_ref().getAudioFormatName.unwrap()(format, buf.as_mut_ptr()) };
@@ -747,7 +748,7 @@ impl API {
747748
} else {
748749
// Find the null terminator and create a CStr from the buffer
749750
let cstr = unsafe { std::ffi::CStr::from_ptr(buf.as_ptr()) };
750-
Some(cstr.to_string_lossy().into_owned())
751+
Some(cstr.to_string_lossy())
751752
}
752753
}
753754

rustsynth/src/core.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
format::VideoFormat,
66
frame::{Frame, FrameContext},
77
log::{log_handler_callback, LogHandle, LogHandler, MessageType},
8-
map::OwnedMap,
8+
map::Map,
99
plugin::Plugin,
1010
};
1111
use bitflags::bitflags;
@@ -208,11 +208,11 @@ impl<'core> CoreRef<'core> {
208208
}
209209

210210
/// Create a video filter using the Filter trait
211-
pub fn create_video_filter<F>(&self, filter: &F) -> Result<OwnedMap<'_>, String>
211+
pub fn create_video_filter<F>(&self, filter: &F) -> Result<Map<'_>, String>
212212
where
213213
F: Filter<'core>,
214214
{
215-
let out = OwnedMap::new();
215+
let out = Map::new();
216216
// Get video info from the filter
217217
let video_info = filter.get_video_info()?;
218218
let dependencies = filter.get_dependencies();
@@ -288,11 +288,11 @@ impl<'core> CoreRef<'core> {
288288
}
289289

290290
/// Create a audio filter using the Filter trait
291-
pub fn create_audio_filter<F>(&self, filter: &F) -> Result<OwnedMap<'_>, String>
291+
pub fn create_audio_filter<F>(&self, filter: &F) -> Result<Map<'_>, String>
292292
where
293293
F: Filter<'core>,
294294
{
295-
let out = OwnedMap::new();
295+
let out = Map::new();
296296
// Get audio info from the filter
297297
let audio_info = filter.get_audio_info()?;
298298
let dependencies = filter.get_dependencies();

rustsynth/src/format/audio.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ impl AudioFormat {
163163
}
164164

165165
pub fn get_name(&self) -> Option<String> {
166-
unsafe { API::get_cached().get_audio_format_name(&self.as_ffi()) }
166+
unsafe {
167+
API::get_cached()
168+
.get_audio_format_name(&self.as_ffi())
169+
.map(|f| f.into_owned())
170+
}
167171
}
168172

169173
pub const STEREO16: Self = Self {

rustsynth/src/frame/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Module for frame related types and functionality.
22
mod enums;
33

4-
use std::{marker::PhantomData, ops::Deref, ptr::NonNull};
4+
use std::{borrow::Cow, marker::PhantomData, ops::Deref, ptr::NonNull};
55

66
use rustsynth_sys as ffi;
77

88
use crate::{
99
api::API,
1010
core::CoreRef,
1111
format::{AudioFormat, MediaType, VideoFormat},
12-
map::{MapRef, MapRefMut, MapResult},
12+
map::{Map, MapResult},
1313
};
1414

1515
// One frame of a clip.
@@ -248,16 +248,16 @@ impl<'core> Frame<'core> {
248248

249249
/// Get read-only access to frame properties
250250
#[inline]
251-
pub fn properties(&self) -> MapRef<'_, 'core> {
251+
pub fn properties(&self) -> Map<'core> {
252252
let map_ptr = unsafe { API::get_cached().get_frame_props_ro(self.handle.as_ref()) };
253-
unsafe { MapRef::from_ptr(map_ptr) }
253+
unsafe { Map::from_ptr(map_ptr) }
254254
}
255255

256256
/// Get read-write access to frame properties (only for owned frames)
257257
#[inline]
258-
pub fn properties_mut(&mut self) -> MapRefMut<'_, 'core> {
258+
pub fn properties_mut(&mut self) -> Map<'core> {
259259
let map_ptr = unsafe { API::get_cached().get_frame_props_rw(self.handle.as_ptr()) };
260-
unsafe { MapRefMut::from_ptr(map_ptr) }
260+
unsafe { Map::from_ptr(map_ptr) }
261261
}
262262

263263
// Standard frame property getters
@@ -384,7 +384,7 @@ impl<'core> Frame<'core> {
384384
}
385385

386386
/// Get picture type (single character describing frame type)
387-
pub fn picture_type(&self) -> Option<String> {
387+
pub fn picture_type(&self) -> Option<Cow<'core, str>> {
388388
unsafe {
389389
self.properties()
390390
.get_string_raw_unchecked(c"_PictType", 0)

rustsynth/src/function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{mem, panic, process};
99

1010
use crate::api::API;
1111
use crate::core::CoreRef;
12-
use crate::map::{Map, MapRef, MapRefMut};
12+
use crate::map::Map;
1313

1414
/// Holds a reference to a function that may be called.
1515
#[derive(Debug, PartialEq, Eq)]
@@ -78,8 +78,8 @@ impl<'core> Function<'core> {
7878
{
7979
let closure = move || {
8080
let core = CoreRef::from_ptr(core);
81-
let in_ = MapRef::from_ptr(in_);
82-
let mut out = MapRefMut::from_ptr(out);
81+
let in_ = Map::from_ptr(in_);
82+
let mut out = Map::from_ptr(out);
8383
let callback = Box::from_raw(user_data as *mut F);
8484

8585
callback(core, &in_, &mut out);

rustsynth/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn api_version() -> i32 {
5959
macro_rules! owned_map {
6060
($({$key:literal: $x:expr }),*) => {
6161
{
62-
let mut temp_map = $crate::map::OwnedMap::new();
62+
let mut temp_map = $crate::map::Map::new();
6363
$(
6464
temp_map.set($key, $x).unwrap();
6565
)*

rustsynth/src/map/data.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ use std::ops::Deref;
33

44
#[derive(Clone, Copy, Debug)]
55
pub enum DataType {
6-
String,
7-
Binary,
8-
Unknown,
6+
String = 1,
7+
Binary = 0,
8+
Unknown = -1,
99
}
1010

11-
pub fn handle_data_hint(hint: i32) -> DataType {
12-
match hint {
13-
x if x == ffi::VSDataTypeHint::dtBinary as i32 => DataType::Binary,
14-
x if x == ffi::VSDataTypeHint::dtUtf8 as i32 => DataType::String,
15-
x if x == ffi::VSDataTypeHint::dtUnknown as i32 => DataType::Unknown,
16-
_ => unreachable!(),
11+
impl DataType {
12+
pub fn from_hint(value: i32) -> Self {
13+
match value {
14+
x if x == ffi::VSDataTypeHint::dtBinary as i32 => DataType::Binary,
15+
x if x == ffi::VSDataTypeHint::dtUtf8 as i32 => DataType::String,
16+
x if x == ffi::VSDataTypeHint::dtUnknown as i32 => DataType::Unknown,
17+
_ => DataType::Unknown,
18+
}
1719
}
1820
}
1921

rustsynth/src/map/iterators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'map, 'elem> Iterator for ValueIter<'map, 'elem, String> {
171171
};
172172
self.index += 1;
173173

174-
Some(value)
174+
Some(value.into_owned())
175175
}
176176

177177
#[inline]

0 commit comments

Comments
 (0)