Skip to main content

Module async_api

Module async_api 

Source
Available on crate feature async only.
Expand description

Async API for VisionKit

Enabled with the async Cargo feature. Every type here is an executor-agnostic Future backed by a C callback fired from a Swift Task { … } thunk.

§Available types

TypeWraps
AsyncImageAnalyzerImageAnalyzer.analyze(imageAt:orientation:configuration:) async throws
AsyncOverlaySubjectsImageAnalysisOverlayView.subjects async and .subject(at:) async

§Platform notes

On macOS the subject APIs (subjects, subject(at:)) live on ImageAnalysisOverlayView (Rust: LiveTextInteraction), not on ImageAnalysis as on iOS. AsyncOverlaySubjects wraps this macOS surface.

DataScannerViewController is an iOS-only API and is not available on macOS. Its multi-delegate live-scan surface is a Tier-2 (Stream) concern in any case. No wrappers are provided here.

§Design

  • Each async Swift API gets a @_cdecl thunk that accepts a C callback (result, error, ctx) and a ctx opaque pointer. The thunk spawns a Swift Task, awaits the Apple API, then fires the callback.
  • The Rust side wraps that in a typed Future newtype backed by AsyncCompletionFuture<T> and maps the String error to VisionKitError.
  • Works with any async executor (pollster, Tokio, async-std, …).

§Example

use visionkit::async_api::AsyncImageAnalyzer;
use visionkit::{ImageAnalysisTypes, ImageAnalyzerConfiguration, ImageOrientation};

let cfg = ImageAnalyzerConfiguration::new(ImageAnalysisTypes::TEXT);
let analysis = AsyncImageAnalyzer::new()?
    .analyze_image_at_path("/path/to/image.png", ImageOrientation::Up, &cfg)?
    .await?;
println!("transcript: {}", analysis.transcript()?);

Structs§

AnalysisSubjectBounds
Bounds rectangle for an ImageAnalysisOverlayView.Subject (macOS) or ImageAnalysisInteraction.Subject (iOS).
AnalyzeImageFuture
Future returned by AsyncImageAnalyzer::analyze_image_at_path.
AsyncImageAnalyzer
True-async entry point for ImageAnalyzer.analyze(imageAt:orientation:configuration:).
AsyncOverlaySubjects
True-async entry point for ImageAnalysisOverlayView.subjects and ImageAnalysisOverlayView.subject(at:) (macOS).
SubjectAtFuture
Future returned by AsyncOverlaySubjects::subject_at.
SubjectsFuture
Future returned by AsyncOverlaySubjects::subjects.

Functions§

block_on
Drive a VisionKit async future to completion while pumping the Obj-C main run loop between polls.