Skip to main content

StoredValue

Trait StoredValue 

Source
pub trait StoredValue:
    Clone
    + Send
    + 'static {
    // Required methods
    fn metadata() -> *const c_void;
    fn field_type() -> &'static [u8] ⓘ;
    fn mangled() -> &'static str;
    fn buffer_bytes() -> usize;
    unsafe fn init_storage(&self, key: &str) -> u64;
    unsafe fn decode(buf: &[u8]) -> Self;
    fn encode(&self) -> Vec<u8> ⓘ;
}
Expand description

A type @AppStorage can hold.

The four AppStorage takes without a RawRepresentable detour. The accessors are generic over Value and so are shared — only the initialiser differs per type, and it differs in ways worth having read rather than guessed. From swiftc -emit-ir:

  AppStorage<Bool>.init   (i1,     i64, ptr, i64)        ; x0=value, x1/x2=key, x3=store
  AppStorage<Int>.init    (i64,    i64, ptr, i64)        ; x0=value, x1/x2=key, x3=store
  AppStorage<Double>.init (double, i64, ptr, i64)        ; d0=value, x0/x1=key, x2=store
  AppStorage<String>.init (i64, ptr, i64, ptr, i64)      ; x0/x1=value, x2/x3=key, x4=store

Double is the one that breaks the pattern: its value travels in a float register, so the key and the store shift down one.

Required Methods§

Source

fn metadata() -> *const c_void

Value.Type — what the accessors want in x1, not AppStorage<Value>’s.

Source

fn field_type() -> &'static [u8] ⓘ

The mangled AppStorage<Self>, NUL-terminated, for the field record.

Source

fn mangled() -> &'static str

Self’s own mangling, for naming Binding<Self>.

Source

fn buffer_bytes() -> usize

How many bytes the accessors read and write.

Source

unsafe fn init_storage(&self, key: &str) -> u64

Build the wrapper, returning its UserDefaultLocation.

§Safety

Calls into SwiftUI; key must be a valid string.

Source

unsafe fn decode(buf: &[u8]) -> Self

Read Self out of an accessor’s @out buffer, consuming whatever the getter handed over.

§Safety

buf must be exactly buffer_bytes of a valid Self, owned at +1.

Source

fn encode(&self) -> Vec<u8> ⓘ

Lay Self out for the setter’s @in argument.

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 StoredValue for String

Source§

fn metadata() -> *const c_void

Source§

fn field_type() -> &'static [u8] ⓘ

Source§

fn mangled() -> &'static str

Source§

fn buffer_bytes() -> usize

Source§

unsafe fn init_storage(&self, key: &str) -> u64

Source§

unsafe fn decode(buf: &[u8]) -> Self

Source§

fn encode(&self) -> Vec<u8> ⓘ

Source§

impl StoredValue for bool

Source§

fn metadata() -> *const c_void

Source§

fn field_type() -> &'static [u8] ⓘ

Source§

fn mangled() -> &'static str

Source§

fn buffer_bytes() -> usize

Source§

unsafe fn init_storage(&self, key: &str) -> u64

Source§

unsafe fn decode(buf: &[u8]) -> Self

Source§

fn encode(&self) -> Vec<u8> ⓘ

Source§

impl StoredValue for f64

Source§

fn metadata() -> *const c_void

Source§

fn field_type() -> &'static [u8] ⓘ

Source§

fn mangled() -> &'static str

Source§

fn buffer_bytes() -> usize

Source§

unsafe fn init_storage(&self, key: &str) -> u64

Source§

unsafe fn decode(buf: &[u8]) -> Self

Source§

fn encode(&self) -> Vec<u8> ⓘ

Source§

impl StoredValue for i64

Source§

fn metadata() -> *const c_void

Source§

fn field_type() -> &'static [u8] ⓘ

Source§

fn mangled() -> &'static str

Source§

fn buffer_bytes() -> usize

Source§

unsafe fn init_storage(&self, key: &str) -> u64

Source§

unsafe fn decode(buf: &[u8]) -> Self

Source§

fn encode(&self) -> Vec<u8> ⓘ

Implementors§