1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*!

Prefix-types are types that derive StableAbi along with the
`#[sabi(kind(Prefix(....)))]` helper attribute.
This is mostly intended for **vtables** and **modules**.

Prefix-types cannot directly be passed through ffi,
instead they must be converted to the type declared with `prefix_ref= Foo_Ref`,
and then pass that instead.

To convert `Foo` to `Foo_Ref` you can use any of (non-exhaustive list):

- `PrefixTypeTrait::leak_into_prefix`:<br>
    Which does the conversion directly,but leaks the value.

- `prefix_type::WithMetadata::new`:<br>
    Use this if you need a compiletime constant.<br>
    First create a `StaticRef<WithMetadata<Self>>` constant using
    the [`staticref`] macro,
    then construct a `Foo_Ref` constant with `Foo_Ref(THE_STATICREF_CONSTANT.as_prefix())`.<br>
    There are two examples of this,
    [for modules](#module_construction),and [for vtables](#vtable_construction)


All the fields in the `DerivingType` can be accessed in `DerivingType_Ref` using
accessor methods named the same as the fields.

# Version compatibility

### Adding fields

To ensure that libraries stay abi compatible,
the first minor version of the library must use the `#[sabi(last_prefix_field)]` attribute on some
field, and every minor version after that must add fields at the end (never moving that attribute).
Changing the field that `#[sabi(last_prefix_field)]` is applied to is a breaking change.

Getter methods for fields after the one to which `#[sabi(last_prefix_field)]` was applied to
will return `Option<FieldType>` by default,because those fields might not exist
(the struct might come from a previous version of the library).
To override how to deal with nonexistent fields,
use the `#[sabi(missing_field())]` attribute,
applied to either the struct or the field.

### Alignment

To ensure that users can define empty vtables/modules that can be extended in
semver compatible versions,
this library forces the struct converted to ffi-safe form to have an alignment at
least that of usize.

You must ensure that newer versions don't change the alignment of the struct,
because that makes it ABI incompatible.

# Grammar Reference

For the grammar reference,you can look at the documentation for
[`#[derive(StableAbi)]`](../../derive.StableAbi.html).

# Examples

###  Example 1

Declaring a Prefix-type.

```

use abi_stable::{
    std_types::{RDuration, RStr},
    StableAbi,
};

#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix(prefix_ref = Module_Ref)))]
#[sabi(missing_field(panic))]
pub struct Module {
    pub lib_name: RStr<'static>,

    #[sabi(last_prefix_field)]
    pub elapsed: extern "C" fn() -> RDuration,

    pub description: RStr<'static>,
}

# fn main(){}


```

In this example:

- `#[sabi(kind(Prefix(prefix_ref= Module_Ref)))]` declares this type as being a prefix-type
    with an ffi-safe pointer called `Module_Ref` to which `Module` can be converted into.

- `#[sabi(missing_field(panic))]`
    makes the field accessors panic when attempting to
    access nonexistent fields instead of the default of returning an `Option<FieldType>`.

- `#[sabi(last_prefix_field)]`means that it is the last field in the struct
    that was defined in the first compatible version of the library
    (0.1.0, 0.2.0, 0.3.0, 1.0.0, 2.0.0 ,etc),
    requiring new fields to always be added below preexisting ones.

<span id="module_construction"></span>
### Constructing a module

This example demonstrates how you can construct a module.

For constructing a vtable, you can look at [the next example](#vtable_construction)

```

use abi_stable::{
    extern_fn_panic_handling,
    prefix_type::{PrefixTypeTrait, WithMetadata},
    staticref,
    std_types::{RDuration, RStr},
    StableAbi,
};

fn main() {
    assert_eq!(MODULE_REF.lib_name().as_str(), "foo");

    assert_eq!(MODULE_REF.elapsed()(1000), RDuration::from_secs(1));

    assert_eq!(MODULE_REF.description().as_str(), "this is a module field");
}

#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix(prefix_ref = Module_Ref)))]
#[sabi(missing_field(panic))]
pub struct Module<T> {
    pub lib_name: RStr<'static>,

    #[sabi(last_prefix_field)]
    pub elapsed: extern "C" fn(T) -> RDuration,

    pub description: RStr<'static>,
}

impl Module<u64> {
    // This macro declares a `StaticRef<WithMetadata<Module<u64>>>` constant.
    staticref!(const MODULE_VAL: WithMetadata<Module<u64>> = WithMetadata::new(
        Module{
            lib_name: RStr::from_str("foo"),
            elapsed,
            description: RStr::from_str("this is a module field"),
        },
    ));
}

const MODULE_REF: Module_Ref<u64> = Module_Ref(Module::MODULE_VAL.as_prefix());

extern "C" fn elapsed(milliseconds: u64) -> RDuration {
    extern_fn_panic_handling! {
        RDuration::from_millis(milliseconds)
    }
}

```

<span id="vtable_construction"></span>
### Constructing a vtable

This example demonstrates how you can construct a vtable.

```rust
use abi_stable::{
    extern_fn_panic_handling,
    marker_type::ErasedObject,
    prefix_type::{PrefixTypeTrait, WithMetadata},
    staticref, StableAbi,
};

fn main() {
    unsafe {
        let vtable = MakeVTable::<u64>::MAKE;
        assert_eq!(
            vtable.get_number()(&3u64 as *const u64 as *const ErasedObject),
            12,
        );
    }
    unsafe {
        let vtable = MakeVTable::<u8>::MAKE;
        assert_eq!(
            vtable.get_number()(&128u8 as *const u8 as *const ErasedObject),
            512,
        );
    }
}

#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix(prefix_ref = VTable_Ref)))]
#[sabi(missing_field(panic))]
pub struct VTable {
    #[sabi(last_prefix_field)]
    pub get_number: unsafe extern "C" fn(*const ErasedObject) -> u64,
}

// A dummy struct, used purely for its associated constants.
struct MakeVTable<T>(T);

impl<T> MakeVTable<T>
where
    T: Copy + Into<u64>,
{
    unsafe extern "C" fn get_number(this: *const ErasedObject) -> u64 {
        extern_fn_panic_handling! {
            (*this.cast::<T>()).into() * 4
        }
    }

    // This macro declares a `StaticRef<WithMetadata<VTable>>` constant.
    staticref! {pub const VAL: WithMetadata<VTable> = WithMetadata::new(
        VTable{get_number: Self::get_number},
    )}

    pub const MAKE: VTable_Ref = VTable_Ref(Self::VAL.as_prefix());
}

```


<span id="example2"></span>
###  Example 2:Declaring a type with a VTable

Here is the implementation of a Box-like type,which uses a vtable that is a prefix type.

```

use std::{
    marker::PhantomData,
    mem::ManuallyDrop,
    ops::{Deref, DerefMut},
};

use abi_stable::{
    extern_fn_panic_handling,
    pointer_trait::{CallReferentDrop, TransmuteElement},
    prefix_type::{PrefixTypeTrait, WithMetadata},
    staticref, StableAbi,
};

/// An ffi-safe `Box<T>`
#[repr(C)]
#[derive(StableAbi)]
pub struct BoxLike<T> {
    data: *mut T,

    vtable: BoxVtable_Ref<T>,

    _marker: PhantomData<T>,
}

impl<T> BoxLike<T> {
    pub fn new(value: T) -> Self {
        let box_ = Box::new(value);

        Self {
            data: Box::into_raw(box_),
            vtable: BoxVtable::VTABLE,
            _marker: PhantomData,
        }
    }

    fn vtable(&self) -> BoxVtable_Ref<T> {
        self.vtable
    }

    /// Extracts the value this owns.
    pub fn into_inner(self) -> T {
        let this = ManuallyDrop::new(self);
        let vtable = this.vtable();
        unsafe {
            // Must copy this before calling `vtable.destructor()`
            // because otherwise it would be reading from a dangling pointer.
            let ret = this.data.read();
            vtable.destructor()(this.data, CallReferentDrop::No);
            ret
        }
    }
}

impl<T> Deref for BoxLike<T> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe { &(*self.data) }
    }
}

impl<T> DerefMut for BoxLike<T> {
    fn deref_mut(&mut self) -> &mut T {
        unsafe { &mut (*self.data) }
    }
}

impl<T> Drop for BoxLike<T> {
    fn drop(&mut self) {
        let vtable = self.vtable();

        unsafe { vtable.destructor()(self.data, CallReferentDrop::Yes) }
    }
}

// `#[sabi(kind(Prefix))]` Declares this type as being a prefix-type,
// generating both of these types:
//
//     - BoxVTable_Prefix`: A struct with the fields up to (and including) the field with the
//     `#[sabi(last_prefix_field)]` attribute.
//
//     - BoxVTable_Ref`: An ffi-safe pointer to a `BoxVtable`, with methods to get
//     `BoxVtable`'s fields.
//
#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix))]
pub(crate) struct BoxVtable<T> {
    /// The `#[sabi(last_prefix_field)]` attribute here means that this is
    /// the last field in this struct that was defined in the
    /// first compatible version of the library
    /// (0.1.0, 0.2.0, 0.3.0, 1.0.0, 2.0.0 ,etc),
    /// requiring new fields to always be added after it.
    ///
    /// The `#[sabi(last_prefix_field)]` attribute would stay on this field until the library
    /// bumps its "major" version,
    /// at which point it would be moved to the last field at the time.
    ///
    #[sabi(last_prefix_field)]
    destructor: unsafe extern "C" fn(*mut T, CallReferentDrop),
}

// This is how ffi-safe pointers to generic prefix types are constructed
// at compile-time.
impl<T> BoxVtable<T> {
    // This macro declares a `StaticRef<WithMetadata<BoxVtable<T>>>` constant.
    //
    // StaticRef represents a reference to data that lives forever,
    // but is not necessarily `'static` according to the type system,
    // eg: `BoxVtable<T>`.
    staticref!(const VTABLE_VAL: WithMetadata<Self> = WithMetadata::new(
        Self{
            destructor:destroy_box::<T>,
        },
    ));

    const VTABLE: BoxVtable_Ref<T> =
        { BoxVtable_Ref(Self::VTABLE_VAL.as_prefix()) };
}

unsafe extern "C" fn destroy_box<T>(v: *mut T, call_drop: CallReferentDrop) {
    extern_fn_panic_handling! {
        let mut box_ = Box::from_raw(v as *mut ManuallyDrop<T>);
        if call_drop == CallReferentDrop::Yes {
            ManuallyDrop::drop(&mut *box_);
        }
        drop(box_);
    }
}

# fn main(){}

```


###  Example 3:module

This declares,initializes,and uses a module.

```
use abi_stable::{
    prefix_type::{PrefixTypeTrait, WithMetadata},
    sabi_extern_fn,
    std_types::RDuration,
    StableAbi,
};

// `#[sabi(kind(Prefix))]` Declares this type as being a prefix-type,
// generating both of these types:
//
//     - PersonMod_Prefix`: A struct with the fields up to (and including) the field with the
//     `#[sabi(last_prefix_field)]` attribute.
//
//     - PersonMod_Ref`:
//      An ffi-safe pointer to a `PersonMod`,with methods to get`PersonMod`'s fields.
//
#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix))]
pub struct PersonMod {
    /// The `#[sabi(last_prefix_field)]` attribute here means that this is
    /// the last field in this struct that was defined in the
    /// first compatible version of the library
    /// (0.1.0, 0.2.0, 0.3.0, 1.0.0, 2.0.0 ,etc),
    /// requiring new fields to always be added below preexisting ones.
    ///
    /// The `#[sabi(last_prefix_field)]` attribute would stay on this field until the library
    /// bumps its "major" version,
    /// at which point it would be moved to the last field at the time.
    ///
    #[sabi(last_prefix_field)]
    pub customer_for: extern "C" fn(Id) -> RDuration,

    // The default behavior for the getter is to return an Option<FieldType>,
    // if the field exists it returns Some(_),
    // otherwise it returns None.
    pub bike_count: extern "C" fn(Id) -> u32,

    // The getter for this field panics if the field doesn't exist.
    #[sabi(missing_field(panic))]
    pub visits: extern "C" fn(Id) -> u32,

    // The getter for this field returns `default_score()` if the field doesn't exist.
    #[sabi(missing_field(with = default_score))]
    pub score: extern "C" fn(Id) -> u32,

    // The getter for this field returns `Default::default()` if the field doesn't exist.
    #[sabi(missing_field(default))]
    pub visit_length: Option<extern "C" fn(Id) -> RDuration>,
}

fn default_score() -> extern "C" fn(Id) -> u32 {
    extern "C" fn default(_: Id) -> u32 {
        1000
    }

    default
}

type Id = u32;

#   static VARS:&[(RDuration,u32)]=&[
#       (RDuration::new(1_000,0),10),
#       (RDuration::new(1_000_000,0),1),
#   ];

#   #[sabi_extern_fn]
#   fn customer_for(id:Id)->RDuration{
#       VARS[id as usize].0
#   }

#   #[sabi_extern_fn]
#   fn bike_count(id:Id)->u32{
#       VARS[id as usize].1
#   }

#   #[sabi_extern_fn]
#   fn visits(id:Id)->u32{
#       VARS[id as usize].1
#   }

#   #[sabi_extern_fn]
#   fn score(id:Id)->u32{
#       VARS[id as usize].1
#   }

/*
    ...
    Elided function definitions
    ...
*/

# fn main(){

const _MODULE_WM_: &WithMetadata<PersonMod> = &WithMetadata::new(
    PersonMod {
        customer_for,
        bike_count,
        visits,
        score,
        visit_length: None,
    },
);

const MODULE: PersonMod_Ref = PersonMod_Ref(_MODULE_WM_.static_as_prefix());

// Getting the value for every field of `MODULE`.

let customer_for: extern "C" fn(Id) -> RDuration = MODULE.customer_for();

let bike_count: Option<extern "C" fn(Id) -> u32> = MODULE.bike_count();

let visits: extern "C" fn(Id) -> u32 = MODULE.visits();

let score: extern "C" fn(Id) -> u32 = MODULE.score();

let visit_length: Option<extern "C" fn(Id) -> RDuration> = MODULE.visit_length();

# }


```









[`staticref`]: ../../macro.staticref.html



*/