Skip to main content

LAEnvironment

Struct LAEnvironment 

Source
pub struct LAEnvironment { /* private fields */ }
Expand description

Managed wrapper around Apple’s LAEnvironment.

Implementations§

Source§

impl LAEnvironment

Source

pub fn current_user() -> Result<Self>

The current user’s authentication environment.

§Errors

Returns an error if the macOS 15 environment APIs are unavailable or the Swift bridge rejects the request.

Examples found in repository?
examples/10_environment.rs (line 8)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let environment = match LAEnvironment::current_user() {
9        Ok(environment) => environment,
10        Err(error) => {
11            println!("environment APIs require macOS 15+: {error}");
12            return Ok(());
13        }
14    };
15
16    let notifications = Arc::new(AtomicUsize::new(0));
17    let registration = environment.add_observer({
18        let notifications = Arc::clone(&notifications);
19        move |environment: &LAEnvironment, old_state: &LAEnvironmentState| {
20            let current_count = environment
21                .state()
22                .and_then(|state| state.all_mechanisms().map(|items| items.len()))
23                .unwrap_or_default();
24            let previous_count = old_state
25                .all_mechanisms()
26                .map(|items| items.len())
27                .unwrap_or_default();
28            println!(
29                "environment changed: {previous_count} -> {current_count} mechanisms"
30            );
31            notifications.fetch_add(1, Ordering::Relaxed);
32        }
33    })?;
34
35    let state = environment.state()?;
36    println!("all mechanisms: {}", state.all_mechanisms()?.len());
37
38    if let Some(biometry) = state.biometry()? {
39        println!(
40            "biometry: {:?}, enrolled={}, usable={}, icon={}",
41            biometry.biometry_type()?,
42            biometry.is_enrolled()?,
43            biometry.is_usable()?,
44            biometry.icon_system_name()?
45        );
46    }
47
48    if let Some(user_password) = state.user_password()? {
49        println!(
50            "user password: set={}, usable={}, label={}",
51            user_password.is_set()?,
52            user_password.is_usable()?,
53            user_password.localized_name()?
54        );
55    }
56
57    for companion in state.companions()? {
58        println!(
59            "companion {:?}: usable={}, state-hash-bytes={}",
60            companion.companion_type()?,
61            companion.is_usable()?,
62            companion.state_hash()?.map_or(0, |hash| hash.len())
63        );
64    }
65
66    environment.remove_observer(&registration)?;
67    println!(
68        "observer notifications seen so far: {}",
69        notifications.load(Ordering::Relaxed)
70    );
71    println!("✅ environment smoke OK");
72    Ok(())
73}
Source

pub fn state(&self) -> Result<LAEnvironmentState>

Snapshot the current environment state.

§Errors

Returns an error if the macOS 15 environment APIs are unavailable or the Swift bridge rejects the request.

Examples found in repository?
examples/10_environment.rs (line 21)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let environment = match LAEnvironment::current_user() {
9        Ok(environment) => environment,
10        Err(error) => {
11            println!("environment APIs require macOS 15+: {error}");
12            return Ok(());
13        }
14    };
15
16    let notifications = Arc::new(AtomicUsize::new(0));
17    let registration = environment.add_observer({
18        let notifications = Arc::clone(&notifications);
19        move |environment: &LAEnvironment, old_state: &LAEnvironmentState| {
20            let current_count = environment
21                .state()
22                .and_then(|state| state.all_mechanisms().map(|items| items.len()))
23                .unwrap_or_default();
24            let previous_count = old_state
25                .all_mechanisms()
26                .map(|items| items.len())
27                .unwrap_or_default();
28            println!(
29                "environment changed: {previous_count} -> {current_count} mechanisms"
30            );
31            notifications.fetch_add(1, Ordering::Relaxed);
32        }
33    })?;
34
35    let state = environment.state()?;
36    println!("all mechanisms: {}", state.all_mechanisms()?.len());
37
38    if let Some(biometry) = state.biometry()? {
39        println!(
40            "biometry: {:?}, enrolled={}, usable={}, icon={}",
41            biometry.biometry_type()?,
42            biometry.is_enrolled()?,
43            biometry.is_usable()?,
44            biometry.icon_system_name()?
45        );
46    }
47
48    if let Some(user_password) = state.user_password()? {
49        println!(
50            "user password: set={}, usable={}, label={}",
51            user_password.is_set()?,
52            user_password.is_usable()?,
53            user_password.localized_name()?
54        );
55    }
56
57    for companion in state.companions()? {
58        println!(
59            "companion {:?}: usable={}, state-hash-bytes={}",
60            companion.companion_type()?,
61            companion.is_usable()?,
62            companion.state_hash()?.map_or(0, |hash| hash.len())
63        );
64    }
65
66    environment.remove_observer(&registration)?;
67    println!(
68        "observer notifications seen so far: {}",
69        notifications.load(Ordering::Relaxed)
70    );
71    println!("✅ environment smoke OK");
72    Ok(())
73}
Source

pub fn add_observer<O>( &self, observer: O, ) -> Result<LAEnvironmentObserverRegistration>

Register an observer for environment state changes.

Keep the returned registration alive for as long as the observer should remain registered. Dropping it releases the weakly-held observer object.

§Errors

Returns an error if the macOS 15 environment APIs are unavailable or the Swift bridge rejects the request.

Examples found in repository?
examples/10_environment.rs (lines 17-33)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let environment = match LAEnvironment::current_user() {
9        Ok(environment) => environment,
10        Err(error) => {
11            println!("environment APIs require macOS 15+: {error}");
12            return Ok(());
13        }
14    };
15
16    let notifications = Arc::new(AtomicUsize::new(0));
17    let registration = environment.add_observer({
18        let notifications = Arc::clone(&notifications);
19        move |environment: &LAEnvironment, old_state: &LAEnvironmentState| {
20            let current_count = environment
21                .state()
22                .and_then(|state| state.all_mechanisms().map(|items| items.len()))
23                .unwrap_or_default();
24            let previous_count = old_state
25                .all_mechanisms()
26                .map(|items| items.len())
27                .unwrap_or_default();
28            println!(
29                "environment changed: {previous_count} -> {current_count} mechanisms"
30            );
31            notifications.fetch_add(1, Ordering::Relaxed);
32        }
33    })?;
34
35    let state = environment.state()?;
36    println!("all mechanisms: {}", state.all_mechanisms()?.len());
37
38    if let Some(biometry) = state.biometry()? {
39        println!(
40            "biometry: {:?}, enrolled={}, usable={}, icon={}",
41            biometry.biometry_type()?,
42            biometry.is_enrolled()?,
43            biometry.is_usable()?,
44            biometry.icon_system_name()?
45        );
46    }
47
48    if let Some(user_password) = state.user_password()? {
49        println!(
50            "user password: set={}, usable={}, label={}",
51            user_password.is_set()?,
52            user_password.is_usable()?,
53            user_password.localized_name()?
54        );
55    }
56
57    for companion in state.companions()? {
58        println!(
59            "companion {:?}: usable={}, state-hash-bytes={}",
60            companion.companion_type()?,
61            companion.is_usable()?,
62            companion.state_hash()?.map_or(0, |hash| hash.len())
63        );
64    }
65
66    environment.remove_observer(&registration)?;
67    println!(
68        "observer notifications seen so far: {}",
69        notifications.load(Ordering::Relaxed)
70    );
71    println!("✅ environment smoke OK");
72    Ok(())
73}
Source

pub fn remove_observer( &self, observer: &LAEnvironmentObserverRegistration, ) -> Result<()>

Remove a previously registered observer.

§Errors

Returns an error if the macOS 15 environment APIs are unavailable or the Swift bridge rejects the request.

Examples found in repository?
examples/10_environment.rs (line 66)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let environment = match LAEnvironment::current_user() {
9        Ok(environment) => environment,
10        Err(error) => {
11            println!("environment APIs require macOS 15+: {error}");
12            return Ok(());
13        }
14    };
15
16    let notifications = Arc::new(AtomicUsize::new(0));
17    let registration = environment.add_observer({
18        let notifications = Arc::clone(&notifications);
19        move |environment: &LAEnvironment, old_state: &LAEnvironmentState| {
20            let current_count = environment
21                .state()
22                .and_then(|state| state.all_mechanisms().map(|items| items.len()))
23                .unwrap_or_default();
24            let previous_count = old_state
25                .all_mechanisms()
26                .map(|items| items.len())
27                .unwrap_or_default();
28            println!(
29                "environment changed: {previous_count} -> {current_count} mechanisms"
30            );
31            notifications.fetch_add(1, Ordering::Relaxed);
32        }
33    })?;
34
35    let state = environment.state()?;
36    println!("all mechanisms: {}", state.all_mechanisms()?.len());
37
38    if let Some(biometry) = state.biometry()? {
39        println!(
40            "biometry: {:?}, enrolled={}, usable={}, icon={}",
41            biometry.biometry_type()?,
42            biometry.is_enrolled()?,
43            biometry.is_usable()?,
44            biometry.icon_system_name()?
45        );
46    }
47
48    if let Some(user_password) = state.user_password()? {
49        println!(
50            "user password: set={}, usable={}, label={}",
51            user_password.is_set()?,
52            user_password.is_usable()?,
53            user_password.localized_name()?
54        );
55    }
56
57    for companion in state.companions()? {
58        println!(
59            "companion {:?}: usable={}, state-hash-bytes={}",
60            companion.companion_type()?,
61            companion.is_usable()?,
62            companion.state_hash()?.map_or(0, |hash| hash.len())
63        );
64    }
65
66    environment.remove_observer(&registration)?;
67    println!(
68        "observer notifications seen so far: {}",
69        notifications.load(Ordering::Relaxed)
70    );
71    println!("✅ environment smoke OK");
72    Ok(())
73}

Trait Implementations§

Source§

impl Debug for LAEnvironment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.