Add Rust std::shared_ptr binding
diff --git a/syntax/check.rs b/syntax/check.rs
index 2ebbf05..6dce563 100644
--- a/syntax/check.rs
+++ b/syntax/check.rs
@@ -32,6 +32,7 @@
Type::RustBox(ptr) => check_type_box(cx, ptr),
Type::RustVec(ty) => check_type_rust_vec(cx, ty),
Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
+ Type::SharedPtr(ptr) => check_type_shared_ptr(cx, ptr),
Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
Type::Ref(ty) => check_type_ref(cx, ty),
Type::Array(array) => check_type_array(cx, array),
@@ -115,6 +116,7 @@
if let Type::Ident(ident) = &ptr.inner {
if cx.types.rust.contains(&ident.rust) {
cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
+ return;
}
match Atom::from(&ident.rust) {
@@ -128,6 +130,29 @@
cx.error(ptr, "unsupported unique_ptr target type");
}
+fn check_type_shared_ptr(cx: &mut Check, ptr: &Ty1) {
+ if let Type::Ident(ident) = &ptr.inner {
+ if cx.types.rust.contains(&ident.rust) {
+ cx.error(ptr, "shared_ptr of a Rust type is not supported yet");
+ return;
+ }
+
+ match Atom::from(&ident.rust) {
+ None => return,
+ Some(CxxString) => {
+ cx.error(ptr, "std::shared_ptr<std::string> is not supported yet");
+ return;
+ }
+ _ => {}
+ }
+ } else if let Type::CxxVector(_) = &ptr.inner {
+ cx.error(ptr, "std::shared_ptr<std::vector> is not supported yet");
+ return;
+ }
+
+ cx.error(ptr, "unsupported shared_ptr target type");
+}
+
fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
if let Type::Ident(ident) = &ptr.inner {
if cx.types.rust.contains(&ident.rust) {
@@ -135,6 +160,7 @@
ptr,
"C++ vector containing a Rust type is not supported yet",
);
+ return;
}
match Atom::from(&ident.rust) {
@@ -410,7 +436,7 @@
return;
}
- if let Type::UniquePtr(ty) | Type::CxxVector(ty) = ty {
+ if let Type::UniquePtr(ty) | Type::SharedPtr(ty) | Type::CxxVector(ty) = ty {
if let Type::Ident(inner) = &ty.inner {
if Atom::from(&inner.rust).is_none() {
return;
@@ -473,6 +499,7 @@
fn check_reserved_name(cx: &mut Check, ident: &Ident) {
if ident == "Box"
|| ident == "UniquePtr"
+ || ident == "SharedPtr"
|| ident == "Vec"
|| ident == "CxxVector"
|| ident == "str"
@@ -493,6 +520,7 @@
Type::RustBox(_)
| Type::RustVec(_)
| Type::UniquePtr(_)
+ | Type::SharedPtr(_)
| Type::Ref(_)
| Type::Str(_)
| Type::SliceRef(_) => false,
@@ -564,6 +592,7 @@
Type::RustBox(_) => "Box".to_owned(),
Type::RustVec(_) => "Vec".to_owned(),
Type::UniquePtr(_) => "unique_ptr".to_owned(),
+ Type::SharedPtr(_) => "shared_ptr".to_owned(),
Type::Ref(_) => "reference".to_owned(),
Type::Str(_) => "&str".to_owned(),
Type::CxxVector(_) => "C++ vector".to_owned(),
diff --git a/syntax/error.rs b/syntax/error.rs
index d0ae021..a672329 100644
--- a/syntax/error.rs
+++ b/syntax/error.rs
@@ -29,7 +29,7 @@
pub static BOX_CXX_TYPE: Error = Error {
msg: "Box of a C++ type is not supported yet",
label: None,
- note: Some("hint: use UniquePtr<>"),
+ note: Some("hint: use UniquePtr<> or SharedPtr<>"),
};
pub static CXXBRIDGE_RESERVED: Error = Error {
@@ -47,7 +47,7 @@
pub static CXX_TYPE_BY_VALUE: Error = Error {
msg: "C++ type by value is not supported",
label: None,
- note: Some("hint: wrap it in a UniquePtr<>"),
+ note: Some("hint: wrap it in a UniquePtr<> or SharedPtr<>"),
};
pub static DISCRIMINANT_OVERFLOW: Error = Error {
diff --git a/syntax/impls.rs b/syntax/impls.rs
index 73cb1d9..9b154f6 100644
--- a/syntax/impls.rs
+++ b/syntax/impls.rs
@@ -45,6 +45,7 @@
Type::Ident(t) => t.hash(state),
Type::RustBox(t) => t.hash(state),
Type::UniquePtr(t) => t.hash(state),
+ Type::SharedPtr(t) => t.hash(state),
Type::Ref(t) => t.hash(state),
Type::Str(t) => t.hash(state),
Type::RustVec(t) => t.hash(state),
@@ -65,6 +66,7 @@
(Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs,
(Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs,
(Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs,
+ (Type::SharedPtr(lhs), Type::SharedPtr(rhs)) => lhs == rhs,
(Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs,
(Type::Str(lhs), Type::Str(rhs)) => lhs == rhs,
(Type::RustVec(lhs), Type::RustVec(rhs)) => lhs == rhs,
diff --git a/syntax/improper.rs b/syntax/improper.rs
index e0e5154..c0f6983 100644
--- a/syntax/improper.rs
+++ b/syntax/improper.rs
@@ -28,7 +28,7 @@
| Type::Fn(_)
| Type::Void(_)
| Type::SliceRef(_) => Definite(true),
- Type::UniquePtr(_) | Type::CxxVector(_) => Definite(false),
+ Type::UniquePtr(_) | Type::SharedPtr(_) | Type::CxxVector(_) => Definite(false),
Type::Ref(ty) => self.determine_improper_ctype(&ty.inner),
Type::Array(ty) => self.determine_improper_ctype(&ty.inner),
}
diff --git a/syntax/mod.rs b/syntax/mod.rs
index e8bd290..12b269e 100644
--- a/syntax/mod.rs
+++ b/syntax/mod.rs
@@ -167,6 +167,7 @@
RustBox(Box<Ty1>),
RustVec(Box<Ty1>),
UniquePtr(Box<Ty1>),
+ SharedPtr(Box<Ty1>),
Ref(Box<Ref>),
Str(Box<Ref>),
CxxVector(Box<Ty1>),
diff --git a/syntax/parse.rs b/syntax/parse.rs
index a3f50ec..e4f0512 100644
--- a/syntax/parse.rs
+++ b/syntax/parse.rs
@@ -822,6 +822,16 @@
rangle: generic.gt_token,
})));
}
+ } else if ident == "SharedPtr" && generic.args.len() == 1 {
+ if let GenericArgument::Type(arg) = &generic.args[0] {
+ let inner = parse_type(arg)?;
+ return Ok(Type::SharedPtr(Box::new(Ty1 {
+ name: ident,
+ langle: generic.lt_token,
+ inner,
+ rangle: generic.gt_token,
+ })));
+ }
} else if ident == "CxxVector" && generic.args.len() == 1 {
if let GenericArgument::Type(arg) = &generic.args[0] {
let inner = parse_type(arg)?;
diff --git a/syntax/tokens.rs b/syntax/tokens.rs
index edbf818..07738d8 100644
--- a/syntax/tokens.rs
+++ b/syntax/tokens.rs
@@ -20,9 +20,11 @@
}
ident.rust.to_tokens(tokens);
}
- Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) | Type::RustVec(ty) => {
- ty.to_tokens(tokens)
- }
+ Type::RustBox(ty)
+ | Type::UniquePtr(ty)
+ | Type::SharedPtr(ty)
+ | Type::CxxVector(ty)
+ | Type::RustVec(ty) => ty.to_tokens(tokens),
Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
Type::Array(a) => a.to_tokens(tokens),
Type::Fn(f) => f.to_tokens(tokens),
@@ -44,7 +46,7 @@
fn to_tokens(&self, tokens: &mut TokenStream) {
let span = self.name.span();
let name = self.name.to_string();
- if let "UniquePtr" | "CxxVector" = name.as_str() {
+ if let "UniquePtr" | "SharedPtr" | "CxxVector" = name.as_str() {
tokens.extend(quote_spanned!(span=> ::cxx::));
} else if name == "Vec" {
tokens.extend(quote_spanned!(span=> ::std::vec::));
diff --git a/syntax/types.rs b/syntax/types.rs
index c98fe85..70b381f 100644
--- a/syntax/types.rs
+++ b/syntax/types.rs
@@ -45,6 +45,7 @@
Type::Ident(_) | Type::Str(_) | Type::Void(_) => {}
Type::RustBox(ty)
| Type::UniquePtr(ty)
+ | Type::SharedPtr(ty)
| Type::CxxVector(ty)
| Type::RustVec(ty) => visit(all, &ty.inner),
Type::Ref(r) => visit(all, &r.inner),
@@ -254,7 +255,7 @@
Atom::from(&ident.rust) == Some(RustString)
}
}
- Type::RustVec(_) | Type::Array(_) => true,
+ Type::SharedPtr(_) | Type::RustVec(_) | Type::Array(_) => true,
_ => false,
}
}