Skip to content

Commit

Permalink
tune api
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Nov 22, 2024
1 parent 25cd53a commit d7ec1b0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
22 changes: 15 additions & 7 deletions cidre/src/av/asset/reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{arc, av, cm, define_cls, define_obj_type, ns, objc};

#[doc(alias = "AVAssetReaderStatus")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(isize)]
pub enum Status {
Expand All @@ -15,21 +16,24 @@ pub enum Status {
Cancelled = 4,
}

define_obj_type!(pub Reader(ns::Id));
define_obj_type!(
#[doc(alias = "AVAssetReader")]
pub Reader(ns::Id)
);

impl arc::A<Reader> {
#[objc::msg_send(initWithAsset:error:)]
pub unsafe fn init_with_assert_err<'ar>(
pub unsafe fn init_with_assert_err<'ear>(
self,
asset: &av::Asset,
error: *mut Option<&'ar ns::Error>,
error: *mut Option<&'ear ns::Error>,
) -> Option<arc::R<Reader>>;
}

impl Reader {
define_cls!(AV_ASSET_READER);

pub fn with_asset<'ar>(asset: &av::Asset) -> Result<arc::R<Reader>, &'ar ns::Error> {
pub fn with_asset<'ear>(asset: &av::Asset) -> ns::Result<'ear, arc::R<Reader>> {
ns::if_none(|err| unsafe { Self::alloc().init_with_assert_err(asset, err) })
}

Expand All @@ -48,7 +52,11 @@ impl Reader {
///
/// This method throws an exception if reading has already started (`status` has progressed beyond AVAssetReaderStatusUnknown).
#[objc::msg_send(startReading)]
pub fn start_reading(&mut self) -> bool;
pub unsafe fn start_reading_throws(&mut self) -> bool;

pub fn start_reading<'ear>(&mut self) -> ns::ExResult<'ear, bool> {
ns::try_catch(|| unsafe { self.start_reading_throws() })
}

/// Cancels any background work and prevents the receiver's outputs from reading more samples.
///
Expand All @@ -62,7 +70,7 @@ impl Reader {
pub fn can_add_output(&self, output: &av::AssetReaderOutput) -> bool;

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

#[objc::msg_send(status)]
pub fn status(&self) -> Status;
Expand All @@ -74,7 +82,7 @@ impl Reader {
pub fn set_time_range(&mut self, value: cm::TimeRange);

#[objc::msg_send(outputs)]
pub fn outputs(&self) -> &ns::Array<av::AssetReaderOutput>;
pub fn outputs(&self) -> arc::R<ns::Array<av::AssetReaderOutput>>;
}

#[link(name = "av", kind = "static")]
Expand Down
18 changes: 5 additions & 13 deletions cidre/src/av/asset/writer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::intrinsics::transmute;

use crate::{arc, av, cm, define_cls, define_obj_type, ns, objc, ut};

use super::WriterInput;

#[doc(alias = "AVAssetWriterStatus")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(isize)]
pub enum Status {
Expand Down Expand Up @@ -79,10 +78,10 @@ impl Writer {
pub fn status(&self) -> Status;

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

#[objc::msg_send(inputs)]
pub fn inputs(&self) -> &ns::Array<WriterInput>;
pub fn inputs(&self) -> arc::R<ns::Array<WriterInput>>;

/// ```
/// use cidre::{av, ns};
Expand All @@ -94,15 +93,8 @@ impl Writer {
pub fn with_url_and_file_type<'ear>(
url: &ns::Url,
file_type: &av::FileType,
) -> Result<arc::R<Self>, &'ear ns::Error> {
let mut error = None;
unsafe {
let res = Self::alloc().init_with_url_file_type_err(url, file_type, &mut error);
match error {
None => Ok(transmute(res)),
Some(e) => Err(e),
}
}
) -> ns::Result<'ear, arc::R<Self>> {
ns::if_none(|err| Self::alloc().init_with_url_file_type_err(url, file_type, err))
}

pub fn with_content_type<'ear>(
Expand Down
16 changes: 9 additions & 7 deletions cidre/src/av/capture/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Device {
///
/// This property can be used for displaying the name of a capture device in a user interface.
#[objc::msg_send(localizedName)]
pub fn localized_name(&self) -> &ns::String;
pub fn localized_name(&self) -> arc::R<ns::String>;

/// The human-readable manufacturer name for the receiver.
///
Expand Down Expand Up @@ -1736,9 +1736,9 @@ impl Device {
media_type: &av::MediaType,
) -> AuthorizationStatus;

pub fn authorization_status_for_media_type<'ar>(
pub fn authorization_status_for_media_type<'ear>(
media_type: &av::MediaType,
) -> Result<AuthorizationStatus, &'ar ns::Exception> {
) -> ns::ExResult<'ear, AuthorizationStatus> {
ns::try_catch(|| unsafe { Self::authorization_status_for_media_type_throws(media_type) })
}

Expand Down Expand Up @@ -1951,7 +1951,7 @@ impl Format {
pub fn video_min_zoom_factor_for_center_stage(&self) -> cg::Float;

#[objc::msg_send(videoFrameRateRangeForCenterStage)]
pub fn video_frame_rate_range_for_center_stage(&self) -> Option<&FrameRateRange>;
pub fn video_frame_rate_range_for_center_stage(&self) -> Option<arc::R<FrameRateRange>>;
}

/// # Portrait Effect
Expand All @@ -1965,7 +1965,7 @@ impl Format {

#[objc::msg_send(videoFrameRateRangeForPortraitEffect)]
#[api::available(macos = 12.0, ios = 15.0, maccatalyst = 15.0, tvos = 17.0)]
pub fn video_frame_rate_range_for_portrait_effect(&self) -> Option<&av::FrameRateRange>;
pub fn video_frame_rate_range_for_portrait_effect(&self) -> Option<arc::R<av::FrameRateRange>>;
}

/// AVCaptureDeviceFormatStudioLight
Expand All @@ -1977,7 +1977,7 @@ impl Format {
/// Devices may support a limited frame rate range when Studio Light is active. If this device format does not support Studio Light, this property returns None.
#[objc::msg_send(videoFrameRateRangeForStudioLight)]
#[api::available(macos = 13.0, ios = 16.0, maccatalyst = 16.0, tvos = 17.0)]
pub fn video_frame_rate_range_for_studio_light(&self) -> Option<&av::FrameRateRange>;
pub fn video_frame_rate_range_for_studio_light(&self) -> Option<arc::R<av::FrameRateRange>>;
}

/// # Determining Color Support
Expand Down Expand Up @@ -2039,7 +2039,9 @@ impl Format {
/// If this device format does not support Background Replacement, this property returns None.
#[objc::msg_send(videoFrameRateRangeForBackgroundReplacement)]
#[api::available(macos = 15.0, ios = 18.0, maccatalyst = 18.0, tvos = 18.0)]
pub fn video_frame_rate_range_for_background_replacement(&self) -> Option<&av::FrameRateRange>;
pub fn video_frame_rate_range_for_background_replacement(
&self,
) -> Option<arc::R<av::FrameRateRange>>;
}

pub mod notifications {
Expand Down
26 changes: 26 additions & 0 deletions cidre/src/ns/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,32 @@ macro_rules! nsstr {

pub use nsstr as str;

impl AsRef<ns::String> for ns::String {
fn as_ref(&self) -> &ns::String {
self
}
}

#[cfg(feature = "cf")]
impl AsRef<cf::String> for ns::String {
fn as_ref(&self) -> &cf::String {
self.as_cf()
}
}

impl AsRef<ns::String> for ns::StringMut {
fn as_ref(&self) -> &ns::String {
self
}
}

#[cfg(feature = "cf")]
impl AsRef<cf::String> for ns::StringMut {
fn as_ref(&self) -> &cf::String {
self.as_cf()
}
}

#[cfg(test)]
mod tests {
use crate::{ns, objc::ar_pool};
Expand Down
6 changes: 6 additions & 0 deletions cidre/src/ns/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ impl From<ns::UInteger> for arc::R<Number> {

impl Eq for Number {}

impl AsRef<ns::Number> for ns::Number {
fn as_ref(&self) -> &ns::Number {
self
}
}

#[link(name = "ns", kind = "static")]
extern "C" {
static NS_VALUE: &'static Class<ns::Value>;
Expand Down

0 comments on commit d7ec1b0

Please sign in to comment.