Skip to content

Commit

Permalink
safer ns::Array api
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Nov 28, 2024
1 parent d9a8b19 commit 8472eb4
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cidre/examples/av-asset-reader/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion cidre/examples/av-asset-writer-hls/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion cidre/examples/av-asset-writer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion cidre/src/av/audio/speech_synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 4 additions & 7 deletions cidre/src/av/audio/unit/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ impl UnitEq {
}

#[objc::msg_send(bands)]
pub fn bands(&self) -> &ns::Array<FilterParameters>;

#[objc::msg_send(bands)]
pub fn bands_mut(&mut self) -> &mut ns::Array<FilterParameters>;
pub fn bands(&self) -> arc::R<ns::Array<FilterParameters>>;

#[objc::msg_send(globalGain)]
pub fn global_gain(&self) -> f32;
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion cidre/src/mtk/texture_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion cidre/src/mtl/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
16 changes: 14 additions & 2 deletions cidre/src/ns/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ impl<T: objc::Obj> Array<T> {
}

#[objc::msg_send(objectAtIndex:)]
pub fn get(&self, index: usize) -> arc::R<T>;
pub unsafe fn get_throws(&self, index: usize) -> arc::R<T>;

pub fn get<'ear>(&self, index: usize) -> ns::ExResult<arc::R<T>> {
unsafe { ns::try_catch(|| self.get_throws(index)) }
}
}

impl<T: objc::Obj> arc::A<ArrayMut<T>> {
Expand Down Expand Up @@ -366,6 +370,8 @@ extern "C" {

#[cfg(test)]
mod tests {
use std::panic::catch_unwind;

use crate::{ns, objc::Obj};

#[test]
Expand All @@ -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() {
Expand All @@ -410,4 +416,10 @@ mod tests {
mut_copy.clear();
assert!(mut_copy.is_empty());
}

#[test]
fn exception() {
let arr = ns::Array::<ns::Number>::new();
arr.get(0).expect_err("Should be exception");
}
}
2 changes: 1 addition & 1 deletion cidre/src/sc/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions cidre/src/un/notification_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl NotificationContent {
pub fn copy_mut(&self) -> arc::Retained<NotificationContentMut>;

#[objc::msg_send(badge)]
pub fn badge(&self) -> Option<&ns::Number>;
pub fn badge(&self) -> Option<arc::R<ns::Number>>;

#[cfg(not(target_os = "tvos"))]
#[objc::msg_send(body)]
Expand All @@ -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<arc::R<un::NotificationSound>>;

#[cfg(not(target_os = "tvos"))]
#[objc::msg_send(subtitle)]
Expand Down

0 comments on commit 8472eb4

Please sign in to comment.