From 8472eb4b958a8399985eae8730477cb02f252e86 Mon Sep 17 00:00:00 2001 From: Yury Date: Thu, 28 Nov 2024 09:46:01 +0300 Subject: [PATCH] safer ns::Array api --- cidre/examples/av-asset-reader/main.rs | 4 ++-- cidre/examples/av-asset-writer-hls/main.rs | 3 ++- cidre/examples/av-asset-writer/main.rs | 3 ++- cidre/src/av/audio/speech_synthesis.rs | 2 +- cidre/src/av/audio/unit/eq.rs | 11 ++++------- cidre/src/mtk/texture_loader.rs | 2 +- cidre/src/mtl/library.rs | 2 +- cidre/src/ns/array.rs | 16 ++++++++++++++-- cidre/src/sc/stream.rs | 2 +- cidre/src/un/notification_content.rs | 4 ++-- 10 files changed, 30 insertions(+), 19 deletions(-) diff --git a/cidre/examples/av-asset-reader/main.rs b/cidre/examples/av-asset-reader/main.rs index 2427d5ff..d22b4e70 100644 --- a/cidre/examples/av-asset-reader/main.rs +++ b/cidre/examples/av-asset-reader/main.rs @@ -20,7 +20,7 @@ async fn main() { ); let mut output = - av::AssetReaderTrackOutput::with_track(&tracks.get(0), Some(&options)).unwrap(); + av::AssetReaderTrackOutput::with_track(&tracks.get(0).unwrap(), Some(&options)).unwrap(); // let mut output = av::AssetReaderTrackOutput::with_track(&tracks[0], None).unwrap(); output.set_always_copies_sample_data(false); @@ -106,7 +106,7 @@ async fn main() { // } if let Some(results) = features.results() { if !results.is_empty() { - let res = results.get(0); + let res = results.get(0).unwrap(); feature_prints.push(res.vec_f32()); // if let Some(prev) = prev_frame_featurs.as_ref() { diff --git a/cidre/examples/av-asset-writer-hls/main.rs b/cidre/examples/av-asset-writer-hls/main.rs index d227f4cb..8a4b137b 100644 --- a/cidre/examples/av-asset-writer-hls/main.rs +++ b/cidre/examples/av-asset-writer-hls/main.rs @@ -137,6 +137,7 @@ impl av::AssetWriterDelegateImpl for WriterDelegate { .unwrap() .track_reports() .get(0) + .unwrap() .duration() .as_secs(); ctx.write_segment(segment_data.as_slice(), duration); @@ -200,7 +201,7 @@ async fn main() { let queue = dispatch::Queue::serial_with_ar_pool(); let content = sc::ShareableContent::current().await.expect("content"); - let display = content.displays().get(0); + let display = content.displays().get(0).unwrap(); let mut cfg = sc::StreamCfg::new(); cfg.set_minimum_frame_interval(cm::Time::new(1, FPS)); cfg.set_width(display.width() as usize * 2); diff --git a/cidre/examples/av-asset-writer/main.rs b/cidre/examples/av-asset-writer/main.rs index 8f46665f..86496e5e 100644 --- a/cidre/examples/av-asset-writer/main.rs +++ b/cidre/examples/av-asset-writer/main.rs @@ -61,7 +61,8 @@ async fn reader_and_output( .await .unwrap(); - let mut track_output = av::AssetReaderTrackOutput::with_track(&tracks.get(0), None).unwrap(); + let mut track_output = + av::AssetReaderTrackOutput::with_track(&tracks.get(0).unwrap(), None).unwrap(); track_output.set_always_copies_sample_data(false); asset_reader.add_output(&track_output).unwrap(); diff --git a/cidre/src/av/audio/speech_synthesis.rs b/cidre/src/av/audio/speech_synthesis.rs index 969436a9..892f3534 100644 --- a/cidre/src/av/audio/speech_synthesis.rs +++ b/cidre/src/av/audio/speech_synthesis.rs @@ -396,7 +396,7 @@ mod tests { assert!(av::SpeechSynthesisVoice::voices().len() > 0); - let v1 = &av::SpeechSynthesisVoice::voices().get(0); + let v1 = &av::SpeechSynthesisVoice::voices().get(0).unwrap(); let _v = av::SpeechSynthesisVoice::with_id(&v1.id()).unwrap(); let _v = av::SpeechSynthesisVoice::with_lang(Some(&v1.lang().unwrap())).unwrap(); diff --git a/cidre/src/av/audio/unit/eq.rs b/cidre/src/av/audio/unit/eq.rs index d3f025c4..66937e32 100644 --- a/cidre/src/av/audio/unit/eq.rs +++ b/cidre/src/av/audio/unit/eq.rs @@ -94,10 +94,7 @@ impl UnitEq { } #[objc::msg_send(bands)] - pub fn bands(&self) -> &ns::Array; - - #[objc::msg_send(bands)] - pub fn bands_mut(&mut self) -> &mut ns::Array; + pub fn bands(&self) -> arc::R>; #[objc::msg_send(globalGain)] pub fn global_gain(&self) -> f32; @@ -117,10 +114,10 @@ mod tests { #[test] fn basics() { - let mut equ = audio::UnitEq::with_bands(10); + let equ = audio::UnitEq::with_bands(10); - let bands = equ.bands_mut(); - bands.get(0).set_gain(10.0); + let bands = equ.bands(); + bands.get(0).unwrap().set_gain(10.0); assert_eq!(bands.len(), 10); assert_eq!(equ.global_gain(), 0.0); } diff --git a/cidre/src/mtk/texture_loader.rs b/cidre/src/mtk/texture_loader.rs index b80c5fdf..788dacbe 100644 --- a/cidre/src/mtk/texture_loader.rs +++ b/cidre/src/mtk/texture_loader.rs @@ -359,6 +359,6 @@ mod tests { assert_eq!(1, textures.len()); assert!(err.is_some()); - assert!(textures.get(0).is_none()); + assert!(textures.get(0).unwrap().is_none()); } } diff --git a/cidre/src/mtl/library.rs b/cidre/src/mtl/library.rs index 887637c1..b92d2469 100644 --- a/cidre/src/mtl/library.rs +++ b/cidre/src/mtl/library.rs @@ -436,7 +436,7 @@ mod tests { let lib = device.new_lib_with_src_blocking(src, None).unwrap(); let names = lib.fn_names(); assert_eq!(1, names.len()); - let n = names.get(0); + let n = names.get(0).unwrap(); let expected_name = ns::str!(c"function_a"); diff --git a/cidre/src/ns/array.rs b/cidre/src/ns/array.rs index 520bbd45..d7132fc3 100644 --- a/cidre/src/ns/array.rs +++ b/cidre/src/ns/array.rs @@ -106,7 +106,11 @@ impl Array { } #[objc::msg_send(objectAtIndex:)] - pub fn get(&self, index: usize) -> arc::R; + pub unsafe fn get_throws(&self, index: usize) -> arc::R; + + pub fn get<'ear>(&self, index: usize) -> ns::ExResult> { + unsafe { ns::try_catch(|| self.get_throws(index)) } + } } impl arc::A> { @@ -366,6 +370,8 @@ extern "C" { #[cfg(test)] mod tests { + use std::panic::catch_unwind; + use crate::{ns, objc::Obj}; #[test] @@ -385,7 +391,7 @@ mod tests { let arr: &[&ns::Number] = &[&one]; let arr = ns::Array::from_slice(arr); assert_eq!(1, arr.len()); - assert_eq!(5, arr.get(0).as_i32()); + assert_eq!(5, arr.first().unwrap().as_i32()); let mut k = 0; for i in arr.iter() { @@ -410,4 +416,10 @@ mod tests { mut_copy.clear(); assert!(mut_copy.is_empty()); } + + #[test] + fn exception() { + let arr = ns::Array::::new(); + arr.get(0).expect_err("Should be exception"); + } } diff --git a/cidre/src/sc/stream.rs b/cidre/src/sc/stream.rs index 978e2f0e..80246a34 100644 --- a/cidre/src/sc/stream.rs +++ b/cidre/src/sc/stream.rs @@ -750,7 +750,7 @@ mod tests { async fn start_fails() { let q = dispatch::Queue::serial_with_ar_pool(); let content = sc::ShareableContent::current().await.expect("content"); - let display = content.displays().get(0); + let display = content.displays().get(0).unwrap(); let mut cfg = sc::StreamCfg::new(); cfg.set_width(display.width() as usize * 2); cfg.set_height(display.height() as usize * 2); diff --git a/cidre/src/un/notification_content.rs b/cidre/src/un/notification_content.rs index f52f8d96..4fd0f5bb 100644 --- a/cidre/src/un/notification_content.rs +++ b/cidre/src/un/notification_content.rs @@ -36,7 +36,7 @@ impl NotificationContent { pub fn copy_mut(&self) -> arc::Retained; #[objc::msg_send(badge)] - pub fn badge(&self) -> Option<&ns::Number>; + pub fn badge(&self) -> Option>; #[cfg(not(target_os = "tvos"))] #[objc::msg_send(body)] @@ -52,7 +52,7 @@ impl NotificationContent { #[cfg(not(target_os = "tvos"))] #[objc::msg_send(sound)] - pub fn sound(&self) -> Option<&un::NotificationSound>; + pub fn sound(&self) -> Option>; #[cfg(not(target_os = "tvos"))] #[objc::msg_send(subtitle)]