Skip to content

Commit 7bddcb2

Browse files
committed
parser: Implement nocopy annotation.
1 parent e24e134 commit 7bddcb2

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/parser.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ struct Annotations {
517517
opaque: bool,
518518
hide: bool,
519519
use_as: Option<String>,
520+
/// Disable deriving copy/clone on this struct.
521+
no_copy: bool,
520522
}
521523

522524
impl Annotations {
@@ -525,6 +527,7 @@ impl Annotations {
525527
opaque: false,
526528
hide: false,
527529
use_as: None,
530+
no_copy: false,
528531
};
529532

530533
anno.parse(&cursor.comment());
@@ -542,6 +545,7 @@ impl Annotations {
542545
"opaque" => self.opaque = true,
543546
"hide" => self.hide = true,
544547
"replaces" => self.use_as = Some(comment.get_tag_attr_value(i)),
548+
"nocopy" => self.no_copy = true,
545549
_ => (),
546550
}
547551
}
@@ -1089,6 +1093,10 @@ fn visit_top(cursor: &Cursor,
10891093
ci.borrow_mut().hide = true;
10901094
}
10911095

1096+
if anno.no_copy {
1097+
ci.borrow_mut().no_copy = true;
1098+
}
1099+
10921100
// If we find a previous translation, we take it now and carry
10931101
// on.
10941102
//

src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ pub struct CompInfo {
429429
pub opaque: bool,
430430
pub base_members: usize,
431431
pub layout: Layout,
432+
/// If this struct is explicitely marked as non-copiable.
433+
pub no_copy: bool,
432434
/// Typedef'd types names, that we'll resolve early to avoid name conflicts
433435
pub typedefs: Vec<String>,
434436
/// If this type has a template parameter which is not a type (e.g.: a size_t)
@@ -479,6 +481,7 @@ impl CompInfo {
479481
hide: false,
480482
parser_cursor: None,
481483
opaque: false,
484+
no_copy: false,
482485
base_members: 0,
483486
layout: layout,
484487
typedefs: vec![],
@@ -570,6 +573,9 @@ impl CompInfo {
570573
}
571574

572575
pub fn can_derive_copy(&self) -> bool {
576+
if self.no_copy {
577+
return false;
578+
}
573579
match self.kind {
574580
CompKind::Union => true,
575581
CompKind::Struct => {

tests/expectations/no_copy.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![feature(const_fn)]
5+
#![allow(non_snake_case)]
6+
7+
8+
/** <div rustbindgen nocopy></div> */
9+
#[repr(C)]
10+
#[derive(Debug)]
11+
pub struct Struct_CopiableButWait<T> {
12+
pub whatever: ::std::os::raw::c_int,
13+
pub _phantom0: ::std::marker::PhantomData<T>,
14+
}

tests/headers/no_copy.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
/** <div rustbindgen nocopy></div> */
3+
template<typename T>
4+
class CopiableButWait {
5+
int whatever;
6+
};

0 commit comments

Comments
 (0)