@@ -48,13 +48,25 @@ impl MetaData {
48
48
49
49
lazy_static ! {
50
50
static ref METADATA : HashMap <u64 , Vec <MetaData >> = {
51
- let file = open_lib_file( ) . expect(
51
+ let file = match open_lib_file( ) {
52
+ Ok ( x) => x,
53
+ Err ( e) => {
54
+ #[ cfg( not( feature = "docs-only" ) ) ]
55
+ panic!(
52
56
r#"
53
57
-- rust-cpp fatal error --
54
58
55
59
Failed to open the target library file.
56
- NOTE: Did you make sure to add the rust-cpp build script?"# ,
57
- ) ;
60
+ NOTE: Did you make sure to add the rust-cpp build script?
61
+ {}"# ,
62
+ e) ;
63
+ #[ cfg( feature = "docs-only" ) ]
64
+ {
65
+ eprintln!( "Error while opening target library: {}" , e) ;
66
+ return Default :: default ( )
67
+ } ;
68
+ }
69
+ } ;
58
70
59
71
read_metadata( file) . expect(
60
72
r#"
@@ -191,10 +203,19 @@ pub fn expand_internal(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
191
203
let size_data = match METADATA . get ( & closure. sig . name_hash ( ) ) {
192
204
Some ( x) => x,
193
205
None => {
206
+ #[ cfg( not( feature = "docs-only" ) ) ]
194
207
return quote ! ( compile_error!{
195
208
r#"This cpp! macro is not found in the library's rust-cpp metadata.
196
209
NOTE: Only cpp! macros found directly in the program source will be parsed -
197
- NOTE: They cannot be generated by macro expansion."# } ) . into ( )
210
+ NOTE: They cannot be generated by macro expansion."# } ) . into ( ) ;
211
+ #[ cfg( feature = "docs-only" ) ]
212
+ {
213
+ return quote ! {
214
+ macro_rules! __cpp_closure_impl {
215
+ ( $( $x: tt) * ) => { panic!( "docs-only" ) ; }
216
+ }
217
+ } . into ( )
218
+ } ;
198
219
}
199
220
} ;
200
221
@@ -362,15 +383,60 @@ pub fn expand_wrap_class(input: proc_macro::TokenStream) -> proc_macro::TokenStr
362
383
} ;
363
384
364
385
let hash = class. name_hash ( ) ;
386
+ let class_name = class. name . clone ( ) ;
365
387
366
388
// Get the size data compiled by the build macro
367
389
let size_data = match METADATA . get ( & hash) {
368
390
Some ( x) => x,
369
391
None => {
392
+ #[ cfg( not( feature = "docs-only" ) ) ]
370
393
return quote ! ( compile_error!{
371
394
r#"This cpp_class! macro is not found in the library's rust-cpp metadata.
372
395
NOTE: Only cpp_class! macros found directly in the program source will be parsed -
373
- NOTE: They cannot be generated by macro expansion."# } ) . into ( )
396
+ NOTE: They cannot be generated by macro expansion."# } ) . into ( ) ;
397
+ #[ cfg( feature = "docs-only" ) ]
398
+ {
399
+ let mut result = quote ! {
400
+ #[ doc( hidden) ]
401
+ impl :: cpp:: CppTrait for #class_name {
402
+ type BaseType = usize ;
403
+ const ARRAY_SIZE : usize = 1 ;
404
+ const CPP_TYPE : & ' static str = stringify!( #class_name) ;
405
+ }
406
+ #[ doc = "NOTE: this trait will only be enabled if the C++ underlying type is trivially copyable" ]
407
+ impl :: std:: marker:: Copy for #class_name { }
408
+ #[ doc = "NOTE: this trait will only be enabled if the C++ underlying type is copyable" ]
409
+ impl :: std:: clone:: Clone for #class_name { fn clone( & self ) -> Self { panic!( "docs-only" ) } }
410
+ #[ doc = "NOTE: this trait will only be enabled if the C++ underlying type is default constructible" ]
411
+ impl :: std:: default :: Default for #class_name { fn default ( ) -> Self { panic!( "docs-only" ) } }
412
+ } ;
413
+ if class. derives ( "PartialEq" ) {
414
+ result = quote ! { #result
415
+ impl :: std:: cmp:: PartialEq for #class_name {
416
+ fn eq( & self , other: & #class_name) -> bool { panic!( "docs-only" ) }
417
+ }
418
+ } ;
419
+ }
420
+ if class. derives ( "PartialOrd" ) {
421
+ result = quote ! { #result
422
+ impl :: std:: cmp:: PartialOrd for #class_name {
423
+ fn partial_cmp( & self , other: & #class_name) -> :: std:: option:: Option <:: std:: cmp:: Ordering > {
424
+ panic!( "docs-only" )
425
+ }
426
+ }
427
+ } ;
428
+ }
429
+ if class. derives ( "Ord" ) {
430
+ result = quote ! { #result
431
+ impl :: std:: cmp:: Ord for #class_name {
432
+ fn cmp( & self , other: & #class_name) -> :: std:: cmp:: Ordering {
433
+ panic!( "docs-only" )
434
+ }
435
+ }
436
+ } ;
437
+ }
438
+ return result. into ( )
439
+ } ;
374
440
}
375
441
} ;
376
442
@@ -387,7 +453,6 @@ NOTE: They cannot be generated by macro expansion."#}).into()
387
453
let destructor_name = Ident :: new ( & format ! ( "__cpp_destructor_{}" , hash) , Span :: call_site ( ) ) ;
388
454
let copyctr_name = Ident :: new ( & format ! ( "__cpp_copy_{}" , hash) , Span :: call_site ( ) ) ;
389
455
let defaultctr_name = Ident :: new ( & format ! ( "__cpp_default_{}" , hash) , Span :: call_site ( ) ) ;
390
- let class_name = class. name . clone ( ) ;
391
456
392
457
let mut result = quote ! {
393
458
#[ doc( hidden) ]
0 commit comments