Implement rust::Vec clear in terms of truncate(0)

This commit is contained in:
David Tolnay 2021-12-31 13:21:57 -08:00
parent a4bc81f48a
commit fa7380675e
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
6 changed files with 7 additions and 41 deletions

View File

@ -1480,11 +1480,6 @@ fn write_rust_vec_extern(out: &mut OutFile, key: NamedImplKey) {
"void cxxbridge1$rust_vec${}$truncate(::rust::Vec<{}> *ptr, ::std::size_t len) noexcept;",
instance, inner,
);
writeln!(
out,
"void cxxbridge1$rust_vec${}$clear(::rust::Vec<{}> *ptr) noexcept;",
instance, inner,
);
}
fn write_rust_box_impl(out: &mut OutFile, key: NamedImplKey) {
@ -1605,16 +1600,6 @@ fn write_rust_vec_impl(out: &mut OutFile, key: NamedImplKey) {
instance,
);
writeln!(out, "}}");
writeln!(out, "template <>");
begin_function_definition(out);
writeln!(out, "void Vec<{}>::clear() {{", inner);
writeln!(
out,
" return cxxbridge1$rust_vec${}$clear(this);",
instance,
);
writeln!(out, "}}");
}
fn write_unique_ptr(out: &mut OutFile, key: NamedImplKey) {

View File

@ -956,6 +956,11 @@ void Vec<T>::emplace_back(Args &&...args) {
this->set_len(size + 1);
}
template <typename T>
void Vec<T>::clear() {
this->truncate(0);
}
template <typename T>
typename Vec<T>::iterator Vec<T>::begin() noexcept {
return Slice<T>(this->data(), this->size()).begin();

View File

@ -1289,7 +1289,6 @@ fn expand_rust_vec(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl
let link_reserve_total = format!("{}reserve_total", link_prefix);
let link_set_len = format!("{}set_len", link_prefix);
let link_truncate = format!("{}truncate", link_prefix);
let link_clear = format!("{}clear", link_prefix);
let local_prefix = format_ident!("{}__vec_", elem);
let local_new = format_ident!("{}new", local_prefix);
@ -1300,7 +1299,6 @@ fn expand_rust_vec(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl
let local_reserve_total = format_ident!("{}reserve_total", local_prefix);
let local_set_len = format_ident!("{}set_len", local_prefix);
let local_truncate = format_ident!("{}truncate", local_prefix);
let local_clear = format_ident!("{}clear", local_prefix);
let (impl_generics, ty_generics) = generics::split_for_impl(key, explicit_impl, resolve);
@ -1360,12 +1358,6 @@ fn expand_rust_vec(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl
let __fn = concat!("<", module_path!(), #prevent_unwind_drop_label);
::cxx::private::prevent_unwind(__fn, || (*this).truncate(len));
}
#[doc(hidden)]
#[export_name = #link_clear]
unsafe extern "C" fn #local_clear #impl_generics(this: *mut ::cxx::private::RustVec<#elem #ty_generics>) {
let __fn = concat!("<", module_path!(), #prevent_unwind_drop_label);
::cxx::private::prevent_unwind(__fn, || (*this).clear());
}
}
}

View File

@ -613,9 +613,7 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *),
void cxxbridge1$rust_vec$##RUST_TYPE##$set_len(rust::Vec<CXX_TYPE> *ptr, \
std::size_t len) noexcept; \
void cxxbridge1$rust_vec$##RUST_TYPE##$truncate(rust::Vec<CXX_TYPE> *ptr, \
std::size_t len) noexcept; \
void cxxbridge1$rust_vec$##RUST_TYPE##$clear( \
rust::Vec<CXX_TYPE> *ptr) noexcept;
std::size_t len) noexcept;
#define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \
template <> \
@ -649,10 +647,6 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *),
template <> \
void Vec<CXX_TYPE>::truncate(std::size_t len) { \
cxxbridge1$rust_vec$##RUST_TYPE##$truncate(this, len); \
} \
template <> \
void Vec<CXX_TYPE>::clear() { \
cxxbridge1$rust_vec$##RUST_TYPE##$clear(this); \
}
#define SHARED_PTR_OPS(RUST_TYPE, CXX_TYPE) \

View File

@ -70,11 +70,7 @@ impl<T> RustVec<T> {
}
pub fn truncate(&mut self, len: usize) {
self.as_mut_vec().truncate(len)
}
pub fn clear(&mut self) {
self.as_mut_vec().clear();
self.as_mut_vec().truncate(len);
}
}

View File

@ -62,12 +62,6 @@ macro_rules! rust_vec_shims {
unsafe { (*this).truncate(len) }
}
}
attr! {
#[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$clear")]
unsafe extern "C" fn __clear(this: *mut RustVec<$ty>) {
unsafe { (*this).clear() }
}
}
};
};
}