Represent mutability checks more concisely
diff --git a/gen/src/write.rs b/gen/src/write.rs
index 5708b49..a014e63 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -344,7 +344,7 @@
let mangled = mangle::extern_fn(efn, out.types);
write!(out, "{}(", mangled);
if let Some(receiver) = &efn.receiver {
- if receiver.mutability.is_none() {
+ if !receiver.mutable {
write!(out, "const ");
}
write!(
@@ -392,7 +392,7 @@
}
write!(out, ")");
if let Some(receiver) = &efn.receiver {
- if receiver.mutability.is_none() {
+ if !receiver.mutable {
write!(out, " const");
}
}
@@ -556,7 +556,7 @@
write!(out, "{}(", link_name);
let mut needs_comma = false;
if let Some(receiver) = &sig.receiver {
- if receiver.mutability.is_none() {
+ if !receiver.mutable {
write!(out, "const ");
}
write!(
@@ -627,7 +627,7 @@
}
write!(out, ")");
if let Some(receiver) = &sig.receiver {
- if receiver.mutability.is_none() {
+ if !receiver.mutable {
write!(out, " const");
}
}
@@ -785,7 +785,7 @@
write!(out, "*");
}
Type::Ref(ty) => {
- if ty.mutability.is_none() {
+ if !ty.mutable {
write!(out, "const ");
}
write_type(out, &ty.inner);
@@ -811,7 +811,7 @@
write!(out, "*");
}
Some(Type::Ref(ty)) => {
- if ty.mutability.is_none() {
+ if !ty.mutable {
write!(out, "const ");
}
write_type(out, &ty.inner);
@@ -871,7 +871,7 @@
write!(out, ">");
}
Type::Ref(r) => {
- if r.mutability.is_none() {
+ if !r.mutable {
write!(out, "const ");
}
write_type(out, &r.inner);
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index fd2ea32..908d8bd 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -316,21 +316,21 @@
},
Type::RustVec(_) => quote!(#var.as_mut_ptr() as *const ::cxx::private::RustVec<_>),
Type::Ref(ty) => match &ty.inner {
- Type::Ident(ident) if ident.rust == RustString => match ty.mutability {
- None => quote!(::cxx::private::RustString::from_ref(#var)),
- Some(_) => quote!(::cxx::private::RustString::from_mut(#var)),
+ Type::Ident(ident) if ident.rust == RustString => match ty.mutable {
+ false => quote!(::cxx::private::RustString::from_ref(#var)),
+ true => quote!(::cxx::private::RustString::from_mut(#var)),
},
- Type::RustVec(vec) if vec.inner == RustString => match ty.mutability {
- None => quote!(::cxx::private::RustVec::from_ref_vec_string(#var)),
- Some(_) => quote!(::cxx::private::RustVec::from_mut_vec_string(#var)),
+ Type::RustVec(vec) if vec.inner == RustString => match ty.mutable {
+ false => quote!(::cxx::private::RustVec::from_ref_vec_string(#var)),
+ true => quote!(::cxx::private::RustVec::from_mut_vec_string(#var)),
},
- Type::RustVec(_) => match ty.mutability {
- None => quote!(::cxx::private::RustVec::from_ref(#var)),
- Some(_) => quote!(::cxx::private::RustVec::from_mut(#var)),
+ Type::RustVec(_) => match ty.mutable {
+ false => quote!(::cxx::private::RustVec::from_ref(#var)),
+ true => quote!(::cxx::private::RustVec::from_mut(#var)),
},
- inner if types.is_considered_improper_ctype(inner) => match ty.mutability {
- None => quote!(#var as *const #inner as *const ::std::ffi::c_void),
- Some(_) => quote!(#var as *mut #inner as *mut ::std::ffi::c_void),
+ inner if types.is_considered_improper_ctype(inner) => match ty.mutable {
+ false => quote!(#var as *const #inner as *const ::std::ffi::c_void),
+ true => quote!(#var as *mut #inner as *mut ::std::ffi::c_void),
},
_ => quote!(#var),
},
@@ -412,17 +412,17 @@
true => quote!(::cxx::UniquePtr::__pin_from_raw(#call)),
},
Type::Ref(ty) => match &ty.inner {
- Type::Ident(ident) if ident.rust == RustString => match ty.mutability {
- None => quote!(#call.as_string()),
- Some(_) => quote!(#call.as_mut_string()),
+ Type::Ident(ident) if ident.rust == RustString => match ty.mutable {
+ false => quote!(#call.as_string()),
+ true => quote!(#call.as_mut_string()),
},
- Type::RustVec(vec) if vec.inner == RustString => match ty.mutability {
- None => quote!(#call.as_vec_string()),
- Some(_) => quote!(#call.as_mut_vec_string()),
+ Type::RustVec(vec) if vec.inner == RustString => match ty.mutable {
+ false => quote!(#call.as_vec_string()),
+ true => quote!(#call.as_mut_vec_string()),
},
- Type::RustVec(_) => match ty.mutability {
- None => quote!(#call.as_vec()),
- Some(_) => quote!(#call.as_mut_vec()),
+ Type::RustVec(_) => match ty.mutable {
+ false => quote!(#call.as_vec()),
+ true => quote!(#call.as_mut_vec()),
},
inner if types.is_considered_improper_ctype(inner) => {
let mutability = ty.mutability;
@@ -594,17 +594,17 @@
true => quote!(::cxx::UniquePtr::__pin_from_raw(#ident)),
},
Type::Ref(ty) => match &ty.inner {
- Type::Ident(i) if i.rust == RustString => match ty.mutability {
- None => quote!(#ident.as_string()),
- Some(_) => quote!(#ident.as_mut_string()),
+ Type::Ident(i) if i.rust == RustString => match ty.mutable {
+ false => quote!(#ident.as_string()),
+ true => quote!(#ident.as_mut_string()),
},
- Type::RustVec(vec) if vec.inner == RustString => match ty.mutability {
- None => quote!(#ident.as_vec_string()),
- Some(_) => quote!(#ident.as_mut_vec_string()),
+ Type::RustVec(vec) if vec.inner == RustString => match ty.mutable {
+ false => quote!(#ident.as_vec_string()),
+ true => quote!(#ident.as_mut_vec_string()),
},
- Type::RustVec(_) => match ty.mutability {
- None => quote!(#ident.as_vec()),
- Some(_) => quote!(#ident.as_mut_vec()),
+ Type::RustVec(_) => match ty.mutable {
+ false => quote!(#ident.as_vec()),
+ true => quote!(#ident.as_mut_vec()),
},
_ => quote!(#ident),
},
@@ -641,17 +641,17 @@
true => Some(quote!(::cxx::UniquePtr::__pin_into_raw)),
},
Type::Ref(ty) => match &ty.inner {
- Type::Ident(ident) if ident.rust == RustString => match ty.mutability {
- None => Some(quote!(::cxx::private::RustString::from_ref)),
- Some(_) => Some(quote!(::cxx::private::RustString::from_mut)),
+ Type::Ident(ident) if ident.rust == RustString => match ty.mutable {
+ false => Some(quote!(::cxx::private::RustString::from_ref)),
+ true => Some(quote!(::cxx::private::RustString::from_mut)),
},
- Type::RustVec(vec) if vec.inner == RustString => match ty.mutability {
- None => Some(quote!(::cxx::private::RustVec::from_ref_vec_string)),
- Some(_) => Some(quote!(::cxx::private::RustVec::from_mut_vec_string)),
+ Type::RustVec(vec) if vec.inner == RustString => match ty.mutable {
+ false => Some(quote!(::cxx::private::RustVec::from_ref_vec_string)),
+ true => Some(quote!(::cxx::private::RustVec::from_mut_vec_string)),
},
- Type::RustVec(_) => match ty.mutability {
- None => Some(quote!(::cxx::private::RustVec::from_ref)),
- Some(_) => Some(quote!(::cxx::private::RustVec::from_mut)),
+ Type::RustVec(_) => match ty.mutable {
+ false => Some(quote!(::cxx::private::RustVec::from_ref)),
+ true => Some(quote!(::cxx::private::RustVec::from_mut)),
},
_ => None,
},
@@ -1087,9 +1087,9 @@
let inner = expand_extern_type(&ty.inner, types, proper);
quote!(&#mutability ::cxx::private::RustVec<#inner>)
}
- inner if proper && types.is_considered_improper_ctype(inner) => match mutability {
- None => quote!(*const ::std::ffi::c_void),
- Some(_) => quote!(*#mutability ::std::ffi::c_void),
+ inner if proper && types.is_considered_improper_ctype(inner) => match ty.mutable {
+ false => quote!(*const ::std::ffi::c_void),
+ true => quote!(*#mutability ::std::ffi::c_void),
},
_ => quote!(#ty),
}
diff --git a/syntax/check.rs b/syntax/check.rs
index 9aaf738..53dd843 100644
--- a/syntax/check.rs
+++ b/syntax/check.rs
@@ -147,7 +147,7 @@
}
fn check_type_ref(cx: &mut Check, ty: &Ref) {
- if ty.mutability.is_some() && !ty.pinned {
+ if ty.mutable && !ty.pinned {
if let Some(requires_pin) = match &ty.inner {
Type::Ident(ident) if ident.rust == CxxString || is_opaque_cxx(cx, &ident.rust) => {
Some(ident.rust.to_string())
@@ -268,9 +268,9 @@
let ref span = span_for_receiver_error(receiver);
if receiver.ty.is_self() {
- let mutability = match receiver.mutability {
- Some(_) => "mut ",
- None => "",
+ let mutability = match receiver.mutable {
+ true => "mut ",
+ false => "",
};
let msg = format!(
"unnamed receiver type is only allowed if the surrounding \
@@ -284,10 +284,7 @@
&& !cx.types.rust.contains(&receiver.ty.rust)
{
cx.error(span, "unrecognized receiver type");
- } else if receiver.mutability.is_some()
- && !receiver.pinned
- && is_opaque_cxx(cx, &receiver.ty.rust)
- {
+ } else if receiver.mutable && !receiver.pinned && is_opaque_cxx(cx, &receiver.ty.rust) {
cx.error(
span,
format!(
@@ -346,13 +343,13 @@
fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
match &efn.ret {
- Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
+ Some(Type::Ref(ty)) if ty.mutable => {}
_ => return,
}
for arg in &efn.args {
if let Type::Ref(ty) = &arg.ty {
- if ty.mutability.is_some() {
+ if ty.mutable {
return;
}
}
diff --git a/syntax/impls.rs b/syntax/impls.rs
index 318cfae..c11c386 100644
--- a/syntax/impls.rs
+++ b/syntax/impls.rs
@@ -124,22 +124,21 @@
pinned,
ampersand: _,
lifetime,
- mutability,
+ mutable,
inner,
pin_tokens: _,
+ mutability: _,
} = self;
let Ref {
pinned: pinned2,
ampersand: _,
lifetime: lifetime2,
- mutability: mutability2,
+ mutable: mutable2,
inner: inner2,
pin_tokens: _,
+ mutability: _,
} = other;
- pinned == pinned2
- && lifetime == lifetime2
- && mutability.is_some() == mutability2.is_some()
- && inner == inner2
+ pinned == pinned2 && lifetime == lifetime2 && mutable == mutable2 && inner == inner2
}
}
@@ -149,13 +148,14 @@
pinned,
ampersand: _,
lifetime,
- mutability,
+ mutable,
inner,
pin_tokens: _,
+ mutability: _,
} = self;
pinned.hash(state);
lifetime.hash(state);
- mutability.is_some().hash(state);
+ mutable.hash(state);
inner.hash(state);
}
}
@@ -246,26 +246,25 @@
pinned,
ampersand: _,
lifetime,
- mutability,
+ mutable,
var: _,
ty,
shorthand: _,
pin_tokens: _,
+ mutability: _,
} = self;
let Receiver {
pinned: pinned2,
ampersand: _,
lifetime: lifetime2,
- mutability: mutability2,
+ mutable: mutable2,
var: _,
ty: ty2,
shorthand: _,
pin_tokens: _,
+ mutability: _,
} = other;
- pinned == pinned2
- && lifetime == lifetime2
- && mutability.is_some() == mutability2.is_some()
- && ty == ty2
+ pinned == pinned2 && lifetime == lifetime2 && mutable == mutable2 && ty == ty2
}
}
@@ -275,15 +274,16 @@
pinned,
ampersand: _,
lifetime,
- mutability,
+ mutable,
var: _,
ty,
shorthand: _,
pin_tokens: _,
+ mutability: _,
} = self;
pinned.hash(state);
lifetime.hash(state);
- mutability.is_some().hash(state);
+ mutable.hash(state);
ty.hash(state);
}
}
diff --git a/syntax/mod.rs b/syntax/mod.rs
index 9d0f829..7f9b918 100644
--- a/syntax/mod.rs
+++ b/syntax/mod.rs
@@ -138,11 +138,12 @@
pub pinned: bool,
pub ampersand: Token![&],
pub lifetime: Option<Lifetime>,
- pub mutability: Option<Token![mut]>,
+ pub mutable: bool,
pub var: Token![self],
pub ty: ResolvableName,
pub shorthand: bool,
pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
+ pub mutability: Option<Token![mut]>,
}
pub struct Variant {
@@ -178,9 +179,10 @@
pub pinned: bool,
pub ampersand: Token![&],
pub lifetime: Option<Lifetime>,
- pub mutability: Option<Token![mut]>,
+ pub mutable: bool,
pub inner: Type,
pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
+ pub mutability: Option<Token![mut]>,
}
pub struct Slice {
diff --git a/syntax/parse.rs b/syntax/parse.rs
index fcc44d2..c4549ea 100644
--- a/syntax/parse.rs
+++ b/syntax/parse.rs
@@ -397,11 +397,12 @@
pinned: false,
ampersand: *ampersand,
lifetime: lifetime.clone(),
- mutability: arg.mutability,
+ mutable: arg.mutability.is_some(),
var: arg.self_token,
ty: ResolvableName::make_self(arg.self_token.span),
shorthand: true,
pin_tokens: None,
+ mutability: arg.mutability,
});
continue;
}
@@ -429,11 +430,12 @@
pinned: reference.pinned,
ampersand: reference.ampersand,
lifetime: reference.lifetime,
- mutability: reference.mutability,
+ mutable: reference.mutable,
var: Token),
ty: ident,
shorthand: false,
pin_tokens: reference.pin_tokens,
+ mutability: reference.mutability,
});
continue;
}
@@ -641,9 +643,10 @@
pinned: false,
ampersand: ty.and_token,
lifetime: ty.lifetime.clone(),
- mutability: ty.mutability,
+ mutable: ty.mutability.is_some(),
inner,
pin_tokens: None,
+ mutability: ty.mutability,
})))
}