abi_stable/sabi_trait/
doc_examples.rs

1//! Examples of [`#[sabi_trait]`](macro@crate::sabi_trait)
2//! generated trait objects,for the documentation.
3
4use crate::sabi_trait;
5
6#[sabi_trait]
7/// An example trait, used to show what [`#[sabi_trait]`](macro@crate::sabi_trait)
8/// generates in the docs.
9#[sabi(use_dyn_trait)]
10pub trait ConstExample: Debug + Clone {
11    ///
12    #[sabi(last_prefix_field)]
13    fn next_number(&self, num: usize) -> usize;
14}
15
16impl ConstExample for usize {
17    fn next_number(&self, num: usize) -> usize {
18        self + num
19    }
20}
21
22#[sabi_trait]
23// #[sabi(debug_print_trait)]
24/// An example trait object that uses `RObject` as a backend.
25pub trait Doer: Debug {
26    ///
27    fn value(&self) -> usize;
28
29    ///
30    fn do_it(&self, num: usize) -> usize;
31
32    ///
33    #[sabi(last_prefix_field)]
34    fn add_into(&mut self, num: usize);
35}
36
37impl Doer for usize {
38    fn value(&self) -> usize {
39        *self
40    }
41
42    fn do_it(&self, num: usize) -> usize {
43        self + num
44    }
45    fn add_into(&mut self, num: usize) {
46        *self += num;
47    }
48}
49
50#[sabi_trait]
51#[doc(hidden)]
52pub trait DocHiddenTrait {}
53
54//////////////////////////////////////////
55
56/// The trait used in examples of [`#[sabi_trait]`](macro@crate::sabi_trait)
57/// trait object methods,
58/// in [`abi_stable::docs::sabi_trait_inherent`]
59#[abi_stable::sabi_trait]
60// #[sabi(debug_print_trait)]
61pub trait Action: Debug {
62    /// Gets the current value of `self`.
63    fn get(&self) -> usize;
64
65    /// Adds `val` into `self`, returning the new value.
66    fn add_mut(&mut self, val: usize) -> usize;
67
68    /// Adds `val` into `self`, returning the new value.
69    #[sabi(last_prefix_field)]
70    fn add_into(self, val: usize) -> usize;
71}
72
73impl Action for usize {
74    fn get(&self) -> usize {
75        *self
76    }
77    fn add_mut(&mut self, val: usize) -> usize {
78        *self += val;
79        *self
80    }
81    fn add_into(mut self, val: usize) -> usize {
82        self += val;
83        self
84    }
85}