Skip to main content

KeyValue

Trait KeyValue 

Source
pub trait KeyValue:
    Clone
    + Send
    + Sync
    + 'static {
    // Required methods
    fn mangled() -> &'static str;
    fn metadata() -> *const c_void;
    unsafe fn write(&self, out: *mut u8);
    unsafe fn read(from: *const u8) -> Self;
}
Expand description

A Swift type a synthesised key can hold.

The three keyed bags — EnvironmentKey, FocusedValueKey, ContainerValueKey — are generic over their value, and so are these. Each implementation is four things: the type’s mangled name (for the key path’s value type), its metadata (for the conformance’s associated type), and the two directions of moving a value across.

§Ownership

write produces a +1 value, because that is what every use here needs: an @in argument is owned by the callee, and a defaultValue witness is called again each time SwiftUI wants the default and destroys what it got. A String that handed back the same object twice without retaining would be over-released, which is why the default is kept as a closure that writes a fresh one rather than as bytes to copy.

read copies and takes nothing: it is reading a value somebody else owns.

Required Methods§

Source

fn mangled() -> &'static str

The mangled name of the Swift type, without the $s.

Source

fn metadata() -> *const c_void

The type’s metadata.

Source

unsafe fn write(&self, out: *mut u8)

Write a +1 copy into a buffer of the type’s stride.

§Safety

out must have room for the type’s stride.

Source

unsafe fn read(from: *const u8) -> Self

Read a copy back, leaving the source owned by whoever owns it.

§Safety

from must address an initialised value of this type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl KeyValue for String

Source§

fn mangled() -> &'static str

Source§

fn metadata() -> *const c_void

Source§

unsafe fn write(&self, out: *mut u8)

Source§

unsafe fn read(from: *const u8) -> Self

Source§

impl KeyValue for bool

Source§

fn mangled() -> &'static str

Source§

fn metadata() -> *const c_void

Source§

unsafe fn write(&self, out: *mut u8)

Source§

unsafe fn read(from: *const u8) -> Self

Source§

impl KeyValue for f64

Source§

fn mangled() -> &'static str

Source§

fn metadata() -> *const c_void

Source§

unsafe fn write(&self, out: *mut u8)

Source§

unsafe fn read(from: *const u8) -> Self

Source§

impl KeyValue for i64

Source§

fn mangled() -> &'static str

Source§

fn metadata() -> *const c_void

Source§

unsafe fn write(&self, out: *mut u8)

Source§

unsafe fn read(from: *const u8) -> Self

Implementors§