From ad30fed23dd4b5591610df3a0a6676c0c66326de Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 19 Nov 2024 21:19:29 +0100 Subject: [PATCH] dedup --- src/mpv/handle.rs | 65 ++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/src/mpv/handle.rs b/src/mpv/handle.rs index 1fabf88..d03bae3 100644 --- a/src/mpv/handle.rs +++ b/src/mpv/handle.rs @@ -129,64 +129,43 @@ impl Handle { Error::from_return_code(unsafe { ffi::mpv_command(self.inner.as_ptr(), args) }) } - pub fn observe_property(&self, reply_userdata: u64, name: &str) -> Result<(), Error> { + // SAFETY: just in case format taking invalid values is Bad + unsafe fn observe_property_impl( + &self, + reply_userdata: u64, + name: &str, + format: ffi::mpv_format, + ) -> Result<(), Error> { let name = CString::new(name).expect("null bytes in property name"); Error::from_return_code(unsafe { - ffi::mpv_observe_property( - self.inner.as_ptr(), - reply_userdata, - name.as_ptr(), - ffi::mpv_format_MPV_FORMAT_NONE, - ) + ffi::mpv_observe_property(self.inner.as_ptr(), reply_userdata, name.as_ptr(), format) }) } + pub fn observe_property(&self, reply_userdata: u64, name: &str) -> Result<(), Error> { + unsafe { self.observe_property_impl(reply_userdata, name, ffi::mpv_format_MPV_FORMAT_NONE) } + } + pub fn observe_property_int64(&self, reply_userdata: u64, name: &str) -> Result<(), Error> { - let name = CString::new(name).expect("null bytes in property name"); - Error::from_return_code(unsafe { - ffi::mpv_observe_property( - self.inner.as_ptr(), - reply_userdata, - name.as_ptr(), - ffi::mpv_format_MPV_FORMAT_INT64, - ) - }) + unsafe { + self.observe_property_impl(reply_userdata, name, ffi::mpv_format_MPV_FORMAT_INT64) + } } pub fn observe_property_double(&self, reply_userdata: u64, name: &str) -> Result<(), Error> { - let name = CString::new(name).expect("null bytes in property name"); - Error::from_return_code(unsafe { - ffi::mpv_observe_property( - self.inner.as_ptr(), - reply_userdata, - name.as_ptr(), - ffi::mpv_format_MPV_FORMAT_DOUBLE, - ) - }) + unsafe { + self.observe_property_impl(reply_userdata, name, ffi::mpv_format_MPV_FORMAT_DOUBLE) + } } pub fn observe_property_string(&self, reply_userdata: u64, name: &str) -> Result<(), Error> { - let name = CString::new(name).expect("null bytes in property name"); - Error::from_return_code(unsafe { - ffi::mpv_observe_property( - self.inner.as_ptr(), - reply_userdata, - name.as_ptr(), - ffi::mpv_format_MPV_FORMAT_STRING, - ) - }) + unsafe { + self.observe_property_impl(reply_userdata, name, ffi::mpv_format_MPV_FORMAT_STRING) + } } pub fn observe_property_flag(&self, reply_userdata: u64, name: &str) -> Result<(), Error> { - let name = CString::new(name).expect("null bytes in property name"); - Error::from_return_code(unsafe { - ffi::mpv_observe_property( - self.inner.as_ptr(), - reply_userdata, - name.as_ptr(), - ffi::mpv_format_MPV_FORMAT_FLAG, - ) - }) + unsafe { self.observe_property_impl(reply_userdata, name, ffi::mpv_format_MPV_FORMAT_FLAG) } } pub fn unobserve_property(&self, registered_reply_userdata: u64) -> Result {