47
47
#![ warn( missing_docs) ]
48
48
#![ doc( html_root_url = "https://docs.rs/openssl-errors/0.1" ) ]
49
49
50
+ use cfg_if:: cfg_if;
50
51
use libc:: { c_char, c_int} ;
51
52
use std:: borrow:: Cow ;
52
53
use std:: marker:: PhantomData ;
@@ -70,19 +71,37 @@ pub trait Library {
70
71
fn id ( ) -> c_int ;
71
72
}
72
73
74
+ cfg_if ! {
75
+ if #[ cfg( ossl300) ] {
76
+ type FunctionInner = * const c_char;
77
+ } else {
78
+ type FunctionInner = c_int;
79
+ }
80
+ }
81
+
73
82
/// A function declaration, parameterized by its error library.
74
- pub struct Function < T > ( c_int , PhantomData < T > ) ;
83
+ pub struct Function < T > ( FunctionInner , PhantomData < T > ) ;
84
+
85
+ // manual impls necessary for the 3.0.0 case
86
+ unsafe impl < T > Sync for Function < T > where T : Sync { }
87
+ unsafe impl < T > Send for Function < T > where T : Send { }
75
88
76
89
impl < T > Function < T > {
77
- /// Creates a function from its raw identifier.
90
+ /// This is not considered a part of the crate's public API, and is subject to change at any time.
91
+ ///
92
+ /// # Safety
93
+ ///
94
+ /// The inner value must be valid for the lifetime of the process.
95
+ #[ doc( hidden) ]
78
96
#[ inline]
79
- pub const fn from_raw ( raw : c_int ) -> Function < T > {
97
+ pub const unsafe fn __from_raw ( raw : FunctionInner ) -> Function < T > {
80
98
Function ( raw, PhantomData )
81
99
}
82
100
83
- /// Returns the function's raw identifier.
101
+ /// This is not considered a part of the crate's public API, and is subject to change at any time.
102
+ #[ doc( hidden) ]
84
103
#[ inline]
85
- pub const fn as_raw ( & self ) -> c_int {
104
+ pub const fn __as_raw ( & self ) -> FunctionInner {
86
105
self . 0
87
106
}
88
107
}
@@ -91,15 +110,17 @@ impl<T> Function<T> {
91
110
pub struct Reason < T > ( c_int , PhantomData < T > ) ;
92
111
93
112
impl < T > Reason < T > {
94
- /// Creates a reason from its raw identifier.
113
+ /// This is not considered a part of the crate's public API, and is subject to change at any time.
114
+ #[ doc( hidden) ]
95
115
#[ inline]
96
- pub const fn from_raw ( raw : c_int ) -> Reason < T > {
116
+ pub const fn __from_raw ( raw : c_int ) -> Reason < T > {
97
117
Reason ( raw, PhantomData )
98
118
}
99
119
100
- /// Returns the reason's raw identifier.
120
+ /// This is not considered a part of the crate's public API, and is subject to change at any time.
121
+ #[ doc( hidden) ]
101
122
#[ inline]
102
- pub const fn as_raw ( & self ) -> c_int {
123
+ pub const fn __as_raw ( & self ) -> c_int {
103
124
self . 0
104
125
}
105
126
}
@@ -119,13 +140,37 @@ pub unsafe fn __put_error<T>(
119
140
) where
120
141
T : Library ,
121
142
{
122
- openssl_sys:: ERR_put_error (
123
- T :: id ( ) ,
124
- func. as_raw ( ) ,
125
- reason. as_raw ( ) ,
126
- file. as_ptr ( ) as * const c_char ,
127
- line as c_int ,
128
- ) ;
143
+ put_error_inner ( T :: id ( ) , func. 0 , reason. 0 , file, line, message)
144
+ }
145
+
146
+ unsafe fn put_error_inner (
147
+ library : c_int ,
148
+ func : FunctionInner ,
149
+ reason : c_int ,
150
+ file : & ' static str ,
151
+ line : u32 ,
152
+ message : Option < Cow < ' static , str > > ,
153
+ ) {
154
+ cfg_if ! {
155
+ if #[ cfg( ossl300) ] {
156
+ openssl_sys:: ERR_new ( ) ;
157
+ openssl_sys:: ERR_set_debug (
158
+ file. as_ptr( ) as * const c_char,
159
+ line as c_int,
160
+ func,
161
+ ) ;
162
+ openssl_sys:: ERR_set_error ( library, reason, ptr:: null( ) ) ;
163
+ } else {
164
+ openssl_sys:: ERR_put_error (
165
+ library,
166
+ func,
167
+ reason,
168
+ file. as_ptr( ) as * const c_char,
169
+ line as c_int,
170
+ ) ;
171
+ }
172
+ }
173
+
129
174
let data = match message {
130
175
Some ( Cow :: Borrowed ( s) ) => Some ( ( s. as_ptr ( ) as * const c_char as * mut c_char , 0 ) ) ,
131
176
Some ( Cow :: Owned ( s) ) => {
@@ -223,31 +268,11 @@ macro_rules! openssl_errors {
223
268
fn id( ) -> $crate:: export:: c_int {
224
269
static INIT : $crate:: export:: Once = $crate:: export:: Once :: new( ) ;
225
270
static mut LIB_NUM : $crate:: export:: c_int = 0 ;
226
- static mut STRINGS : [
227
- $crate:: export:: ERR_STRING_DATA ;
228
- 2 + $crate:: openssl_errors!( @count $( $func_name; ) * $( $reason_name; ) * )
229
- ] = [
230
- $crate:: export:: ERR_STRING_DATA {
231
- error: 0 ,
232
- string: concat!( $lib_str, "\0 " ) . as_ptr( ) as * const $crate:: export:: c_char,
233
- } ,
234
- $(
235
- $crate:: export:: ERR_STRING_DATA {
236
- error: $crate:: export:: ERR_PACK ( 0 , $lib_name:: $func_name. as_raw( ) , 0 ) ,
237
- string: concat!( $func_str, "\0 " ) . as_ptr( ) as * const $crate:: export:: c_char,
238
- } ,
239
- ) *
240
- $(
241
- $crate:: export:: ERR_STRING_DATA {
242
- error: $crate:: export:: ERR_PACK ( 0 , 0 , $lib_name:: $reason_name. as_raw( ) ) ,
243
- string: concat!( $reason_str, "\0 " ) . as_ptr( ) as * const $crate:: export:: c_char,
244
- } ,
245
- ) *
246
- $crate:: export:: ERR_STRING_DATA {
247
- error: 0 ,
248
- string: $crate:: export:: null( ) ,
249
- }
250
- ] ;
271
+ $crate:: __openssl_errors_helper! {
272
+ @strings $lib_name( $lib_str)
273
+ functions { $( $func_name( $func_str) ; ) * }
274
+ reasons { $( $reason_name( $reason_str) ; ) * }
275
+ }
251
276
252
277
unsafe {
253
278
INIT . call_once( || {
@@ -263,19 +288,21 @@ macro_rules! openssl_errors {
263
288
}
264
289
265
290
impl $lib_name {
266
- $crate:: openssl_errors!( @func_consts $lib_name; 1 ; $( $( #[ $func_attr] ) * $func_name; ) * ) ;
291
+ $crate:: openssl_errors!( @func_consts $lib_name; 1 ; $( $( #[ $func_attr] ) * $func_name( $func_str ) ; ) * ) ;
267
292
$crate:: openssl_errors!( @reason_consts $lib_name; 1 ; $( $( #[ $reason_attr] ) * $reason_name; ) * ) ;
268
293
}
269
294
) * } ;
270
- ( @func_consts $lib_name: ident; $n: expr; $( #[ $attr: meta] ) * $name: ident; $( $tt: tt) * ) => {
295
+ ( @func_consts $lib_name: ident; $n: expr; $( #[ $attr: meta] ) * $name: ident( $str : expr ) ; $( $tt: tt) * ) => {
271
296
$( #[ $attr] ) *
272
- pub const $name: $crate:: Function <$lib_name> = $crate:: Function :: from_raw( $n) ;
297
+ pub const $name: $crate:: Function <$lib_name> = unsafe {
298
+ $crate:: Function :: __from_raw( $crate:: __openssl_errors_helper!( @func_value $n, $str) )
299
+ } ;
273
300
$crate:: openssl_errors!( @func_consts $lib_name; $n + 1 ; $( $tt) * ) ;
274
301
} ;
275
302
( @func_consts $lib_name: ident; $n: expr; ) => { } ;
276
303
( @reason_consts $lib_name: ident; $n: expr; $( #[ $attr: meta] ) * $name: ident; $( $tt: tt) * ) => {
277
304
$( #[ $attr] ) *
278
- pub const $name: $crate:: Reason <$lib_name> = $crate:: Reason :: from_raw ( $n) ;
305
+ pub const $name: $crate:: Reason <$lib_name> = $crate:: Reason :: __from_raw ( $n) ;
279
306
$crate:: openssl_errors!( @reason_consts $lib_name; $n + 1 ; $( $tt) * ) ;
280
307
} ;
281
308
( @reason_consts $lib_name: ident; $n: expr; ) => { } ;
@@ -284,3 +311,77 @@ macro_rules! openssl_errors {
284
311
} ;
285
312
( @count) => { 0 } ;
286
313
}
314
+
315
+ cfg_if ! {
316
+ if #[ cfg( ossl300) ] {
317
+ #[ doc( hidden) ]
318
+ #[ macro_export]
319
+ macro_rules! __openssl_errors_helper {
320
+ (
321
+ @strings $lib_name: ident( $lib_str: expr)
322
+ functions { $( $func_name: ident( $func_str: expr) ; ) * }
323
+ reasons { $( $reason_name: ident( $reason_str: expr) ; ) * }
324
+ ) => {
325
+ static mut STRINGS : [
326
+ $crate :: export:: ERR_STRING_DATA ;
327
+ 2 + $crate :: openssl_errors!( @count $( $reason_name; ) * )
328
+ ] = [
329
+ $crate :: export:: ERR_STRING_DATA {
330
+ error: 0 ,
331
+ string: concat!( $lib_str, "\0 " ) . as_ptr( ) as * const $crate :: export:: c_char,
332
+ } ,
333
+ $(
334
+ $crate :: export:: ERR_STRING_DATA {
335
+ error: $crate :: export:: ERR_PACK ( 0 , 0 , $lib_name:: $reason_name. __as_raw( ) ) ,
336
+ string: concat!( $reason_str, "\0 " ) . as_ptr( ) as * const $crate :: export:: c_char,
337
+ } ,
338
+ ) *
339
+ $crate :: export:: ERR_STRING_DATA {
340
+ error: 0 ,
341
+ string: $crate :: export:: null( ) ,
342
+ }
343
+ ] ;
344
+ } ;
345
+ ( @func_value $n: expr, $func_str: expr) => {
346
+ concat!( $func_str, "\0 " ) . as_ptr( ) as * const $crate :: export:: c_char
347
+ } ;
348
+ }
349
+ } else {
350
+ #[ doc( hidden) ]
351
+ #[ macro_export]
352
+ macro_rules! __openssl_errors_helper {
353
+ (
354
+ @strings $lib_name: ident( $lib_str: expr)
355
+ functions { $( $func_name: ident( $func_str: expr) ; ) * }
356
+ reasons { $( $reason_name: ident( $reason_str: expr) ; ) * }
357
+ ) => {
358
+ static mut STRINGS : [
359
+ $crate :: export:: ERR_STRING_DATA ;
360
+ 2 + $crate :: openssl_errors!( @count $( $func_name; ) * $( $reason_name; ) * )
361
+ ] = [
362
+ $crate :: export:: ERR_STRING_DATA {
363
+ error: 0 ,
364
+ string: concat!( $lib_str, "\0 " ) . as_ptr( ) as * const $crate :: export:: c_char,
365
+ } ,
366
+ $(
367
+ $crate :: export:: ERR_STRING_DATA {
368
+ error: $crate :: export:: ERR_PACK ( 0 , $lib_name:: $func_name. __as_raw( ) , 0 ) ,
369
+ string: concat!( $func_str, "\0 " ) . as_ptr( ) as * const $crate :: export:: c_char,
370
+ } ,
371
+ ) *
372
+ $(
373
+ $crate :: export:: ERR_STRING_DATA {
374
+ error: $crate :: export:: ERR_PACK ( 0 , 0 , $lib_name:: $reason_name. __as_raw( ) ) ,
375
+ string: concat!( $reason_str, "\0 " ) . as_ptr( ) as * const $crate :: export:: c_char,
376
+ } ,
377
+ ) *
378
+ $crate :: export:: ERR_STRING_DATA {
379
+ error: 0 ,
380
+ string: $crate :: export:: null( ) ,
381
+ }
382
+ ] ;
383
+ } ;
384
+ ( @func_value $n: expr, $func_str: expr) => { $n} ;
385
+ }
386
+ }
387
+ }
0 commit comments