Require shared refs to be mutable

This commit is contained in:
Benjamin Kampmann 2018-09-03 13:27:24 +02:00
parent 8b08fc16c5
commit 3076e40b21
2 changed files with 16 additions and 16 deletions

View File

@ -441,7 +441,7 @@ impl<'a> IdlType<'a> {
| IdlType::DomString
| IdlType::ByteString
| IdlType::UsvString => match pos {
TypePosition::Argument => Some(shared_ref(ident_ty(raw_ident("str")))),
TypePosition::Argument => Some(shared_ref(ident_ty(raw_ident("str")), true)),
TypePosition::Return => Some(ident_ty(raw_ident("String"))),
},
IdlType::Object => {
@ -456,15 +456,15 @@ impl<'a> IdlType<'a> {
Some(leading_colon_path_ty(path))
},
IdlType::DataView => None,
IdlType::Int8Array => Some(array("i8", pos)),
IdlType::Uint8Array => Some(array("u8", pos)),
IdlType::Int8Array => Some(array("i8", pos, true)),
IdlType::Uint8Array => Some(array("u8", pos, true)),
IdlType::Uint8ClampedArray => None, // FIXME(#421)
IdlType::Int16Array => Some(array("i16", pos)),
IdlType::Uint16Array => Some(array("u16", pos)),
IdlType::Int32Array => Some(array("i32", pos)),
IdlType::Uint32Array => Some(array("u32", pos)),
IdlType::Float32Array => Some(array("f32", pos)),
IdlType::Float64Array => Some(array("f64", pos)),
IdlType::Int16Array => Some(array("i16", pos, true)),
IdlType::Uint16Array => Some(array("u16", pos, true)),
IdlType::Int32Array => Some(array("i32", pos, true)),
IdlType::Uint32Array => Some(array("u32", pos, true)),
IdlType::Float32Array => Some(array("f32", pos, true)),
IdlType::Float64Array => Some(array("f64", pos, true)),
IdlType::ArrayBufferView | IdlType::BufferSource => {
let path = vec![rust_ident("js_sys"), rust_ident("Object")];
@ -474,7 +474,7 @@ impl<'a> IdlType<'a> {
| IdlType::Dictionary(name) => {
let ty = ident_ty(rust_ident(camel_case_ident(name).as_str()));
if pos == TypePosition::Argument {
Some(shared_ref(ty))
Some(shared_ref(ty, true))
} else {
Some(ty)
}
@ -488,7 +488,7 @@ impl<'a> IdlType<'a> {
let path = vec![rust_ident("js_sys"), rust_ident("Promise")];
let ty = leading_colon_path_ty(path);
if pos == TypePosition::Argument {
Some(shared_ref(ty))
Some(shared_ref(ty, true))
} else {
Some(ty)
}

View File

@ -14,11 +14,11 @@ use first_pass::{FirstPassRecord, OperationId, OperationData, Signature};
use idl_type::{IdlType, ToIdlType};
/// Take a type and create an immutable shared reference to that type.
pub(crate) fn shared_ref(ty: syn::Type) -> syn::Type {
pub(crate) fn shared_ref(ty: syn::Type, mutable: bool) -> syn::Type {
syn::TypeReference {
and_token: Default::default(),
lifetime: None,
mutability: None,
mutability: if mutable { Some(syn::token::Mut::default()) } else { None },
elem: Box::new(ty),
}.into()
}
@ -57,10 +57,10 @@ pub fn mdn_doc(class: &str, method: Option<&str>) -> String {
}
// Array type is borrowed for arguments (`&[T]`) and owned for return value (`Vec<T>`).
pub(crate) fn array(base_ty: &str, pos: TypePosition) -> syn::Type {
pub(crate) fn array(base_ty: &str, pos: TypePosition, mutable: bool) -> syn::Type {
match pos {
TypePosition::Argument => {
shared_ref(slice_ty(ident_ty(raw_ident(base_ty))))
shared_ref(slice_ty(ident_ty(raw_ident(base_ty))), mutable)
}
TypePosition::Return => {
vec_ty(ident_ty(raw_ident(base_ty)))
@ -240,7 +240,7 @@ impl<'src> FirstPassRecord<'src> {
..
} = &kind {
let mut res = Vec::with_capacity(idl_arguments.size_hint().0 + 1);
res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())));
res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone(), true)));
res
} else {
Vec::with_capacity(idl_arguments.size_hint().0)