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

/**
Use this when constructing a `abi_stable::abi_stability::TypeLayoutParams`
when manually implementing StableAbi,
to more ergonomically initialize the generics field.

# Example 

A type with lifetime type,and lifetime parameters:

```
use abi_stable::{
    tl_genparams,
    StableAbi,
    abi_stability::type_layout::GenericParams
};

struct Reference<'a,'b,T,U>(&'a T,&'b U);

impl<'a,'b,T,U> Reference<'a,'b,T,U>
where
    T:StableAbi,
    U:StableAbi,
{
    const GENERICS:GenericParams=
        tl_genparams!('a,'b ; T,U ; );
}


```

# Example

A type with lifetime,type,and const parameters.

Note that while this example won't compile until const parameters are usable,

```ignore
use abi_stable::{
    tl_genparams,
    StableAbi,
    abi_stability::type_layout::GenericParams
};

struct ArrayReference<'a,'b,T,U,const SIZE_T:usize,const SIZE_U:usize>{
    first:&'a [T;SIZE_T],
    second:&'b [U;SIZE_U],
}

impl<'a,'b,T,U,const SIZE_T:usize,const SIZE_U:usize> 
    Reference<'a,'b,T,U,SIZE_T,SIZE_U>
where
    T:StableAbi,
    U:StableAbi,
{
    const GENERICS:GenericParams=
        tl_genparams!('a,'b ; T,U ; SIZE_T,SIZE_U );
}

```



*/
#[macro_export]
macro_rules! tl_genparams {
    ( $($lt:lifetime),*  ; $($ty:ty),*  ; $($const_p:expr),*  ) => ({
        #[allow(unused_imports)]
        use $crate::{
            abi_stability::{
                stable_abi_trait::SharedStableAbi,
                type_layout::GenericParams
            },
            std_types::StaticStr,
        };

        GenericParams::new(
            &[$( StaticStr::new( stringify!($lt) ) ,)*],
            &[$( <$ty as SharedStableAbi>::S_LAYOUT ,)*],
            &[$( StaticStr::new( stringify!($const_p) ) ,)*],
        )
    })
}




///////////////////////////////////////////////////////////////////////


/// Equivalent to `?` for `RResult<_,_>`.
#[macro_export]
macro_rules! rtry {
    ($expr:expr) => {{
        use $crate::result::{RErr, ROk};
        match $expr.into() {
            ROk(x) => x,
            RErr(x) => return RErr(From::from(x)),
        }
    }};
}

/// Equivalent to `?` for `ROption<_>`.
#[macro_export]
macro_rules! rtry_opt {
    ($expr:expr) => {{
        use $crate::option::{RNone, RSome};
        match $expr.into() {
            RSome(x) => x,
            RNone => return RNone,
        }
    }};
}



///////////////////////////////////////////////////////////////////////


macro_rules! make_rve_utypeid {
    ($ty:ty) => (
        $crate::return_value_equality::ReturnValueEquality{
            function:$crate::std_types::utypeid::new_utypeid::<$ty>
        }
    )
}



///////////////////////////////////////////////////////////////////////




/**
Use this to make sure that you handle panics inside `extern fn` correctly.

This macro causes an abort if a panic reaches this point.

It does not prevent functions inside from using `::std::panic::catch_unwind` to catch the panic.

# Example 

```
use std::fmt;

use abi_stable::{
    extern_fn_panic_handling,
    std_types::RString,
};


pub extern "C" fn print_debug<T>(this: &T,buf: &mut RString)
where
    T: fmt::Debug,
{
    extern_fn_panic_handling! {
        use std::fmt::Write;

        println!("{:?}",this);
    }
}


```


*/
#[macro_export]
macro_rules! extern_fn_panic_handling {
    ( $($fn_contents:tt)* ) => ({
        use std::panic::{self,AssertUnwindSafe};

        let result=panic::catch_unwind(AssertUnwindSafe(move||{
            $($fn_contents)*
        }));

        match result {
            Ok(x)=>x,
            Err(_)=>$crate::utils::ffi_panic_message(file!(),line!()),
        }
    })
}


///////////////////////////////////////////////////////////////////////



/**

Implements the abi_stable::type_info::GetTypeInfo trait for some type.

It's necessary for the type to be `'static` because this uses TypeId.

# Example

```
use abi_stable::{
    impl_get_type_info,
    erased_types::{TypeInfo,ImplType},
};

#[derive(Default, Clone, Debug)]
struct Foo<T> {
    l: u32,
    r: u32,
    name: T,
}

impl<T> ImplType for Foo<T>
where T:'static+Send+Sync
{
    type Interface=();
    
    // You have to write the full type (eg: impl_get_type_info!{ Bar['a,T,U] } ) ,
    // never write Self.
    const INFO:&'static TypeInfo=impl_get_type_info! { Foo[T] };
}

```

*/
#[macro_export]
macro_rules! impl_get_type_info {
    (
        $type:ident $([$($params:tt)*])?
    ) => (
        {
            use std::mem;
            use $crate::{
                erased_types::type_info::TypeInfo,
                version::{VersionStrings},
                std_types::{StaticStr,utypeid::new_utypeid},
                return_value_equality::ReturnValueEquality,
            };

            &TypeInfo{
                size:mem::size_of::<Self>(),
                alignment:mem::align_of::<Self>(),
                uid:ReturnValueEquality{
                    function:new_utypeid::<Self>
                },
                name:StaticStr::new(stringify!($type)),
                file:StaticStr::new(file!()),
                package:StaticStr::new(env!("CARGO_PKG_NAME")),
                package_version:VersionStrings{
                    major:StaticStr::new(env!("CARGO_PKG_VERSION_MAJOR")),
                    minor:StaticStr::new(env!("CARGO_PKG_VERSION_MINOR")),
                    patch:StaticStr::new(env!("CARGO_PKG_VERSION_PATCH")),
                },
                _private_field:(),
            }
        }
    )
}