mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2024-11-23 23:29:50 +00:00
Add std::initializer_list constructor for rust::Vec
This commit is contained in:
parent
861e476a66
commit
f799b3781c
@ -6,6 +6,7 @@
|
||||
```cpp,hidelines
|
||||
// rust/cxx.h
|
||||
#
|
||||
# #include <initializer_list>
|
||||
# #include <iterator>
|
||||
# #include <type_traits>
|
||||
#
|
||||
@ -17,6 +18,7 @@ public:
|
||||
using value_type = T;
|
||||
|
||||
Vec() noexcept;
|
||||
Vec(std::initializer_list<T>);
|
||||
Vec(Vec &&) noexcept;
|
||||
~Vec() noexcept;
|
||||
|
||||
|
@ -71,7 +71,9 @@ pub(super) fn write(out: &mut OutFile) {
|
||||
}
|
||||
|
||||
if builtin.rust_vec {
|
||||
include.algorithm = true;
|
||||
include.array = true;
|
||||
include.initializer_list = true;
|
||||
include.iterator = true;
|
||||
include.new = true;
|
||||
include.type_traits = true;
|
||||
|
@ -21,11 +21,13 @@ pub struct Include {
|
||||
#[derive(Default, PartialEq)]
|
||||
pub struct Includes<'a> {
|
||||
pub custom: Vec<Include>,
|
||||
pub algorithm: bool,
|
||||
pub array: bool,
|
||||
pub cstddef: bool,
|
||||
pub cstdint: bool,
|
||||
pub cstring: bool,
|
||||
pub exception: bool,
|
||||
pub initializer_list: bool,
|
||||
pub iterator: bool,
|
||||
pub memory: bool,
|
||||
pub new: bool,
|
||||
@ -69,11 +71,13 @@ pub(super) fn write(out: &mut OutFile) {
|
||||
|
||||
let Includes {
|
||||
custom: _,
|
||||
algorithm,
|
||||
array,
|
||||
cstddef,
|
||||
cstdint,
|
||||
cstring,
|
||||
exception,
|
||||
initializer_list,
|
||||
iterator,
|
||||
memory,
|
||||
new,
|
||||
@ -85,6 +89,9 @@ pub(super) fn write(out: &mut OutFile) {
|
||||
content: _,
|
||||
} = *include;
|
||||
|
||||
if algorithm {
|
||||
writeln!(out, "#include <algorithm>");
|
||||
}
|
||||
if array {
|
||||
writeln!(out, "#include <array>");
|
||||
}
|
||||
@ -100,6 +107,9 @@ pub(super) fn write(out: &mut OutFile) {
|
||||
if exception {
|
||||
writeln!(out, "#include <exception>");
|
||||
}
|
||||
if initializer_list {
|
||||
writeln!(out, "#include <initializer_list>");
|
||||
}
|
||||
if iterator {
|
||||
writeln!(out, "#include <iterator>");
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <initializer_list>
|
||||
#include <iosfwd>
|
||||
#include <iterator>
|
||||
#include <new>
|
||||
@ -238,6 +240,7 @@ public:
|
||||
using value_type = T;
|
||||
|
||||
Vec() noexcept;
|
||||
Vec(std::initializer_list<T>);
|
||||
Vec(Vec &&) noexcept;
|
||||
~Vec() noexcept;
|
||||
|
||||
@ -619,6 +622,12 @@ Box<T>::Box() noexcept = default;
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_VEC
|
||||
#define CXXBRIDGE1_RUST_VEC
|
||||
template <typename T>
|
||||
Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
|
||||
this->reserve_total(init.size());
|
||||
std::move(init.begin(), init.end(), std::back_inserter(*this));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
|
||||
new (&other) Vec();
|
||||
|
@ -132,7 +132,8 @@ const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
|
||||
std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }
|
||||
|
||||
rust::Vec<uint8_t> c_return_rust_vec() {
|
||||
throw std::runtime_error("unimplemented");
|
||||
rust::Vec<uint8_t> vec{2, 0, 2, 0};
|
||||
return vec;
|
||||
}
|
||||
|
||||
const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
|
||||
@ -146,7 +147,7 @@ rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
|
||||
}
|
||||
|
||||
rust::Vec<rust::String> c_return_rust_vec_string() {
|
||||
throw std::runtime_error("unimplemented");
|
||||
return {"2", "0", "2", "0"};
|
||||
}
|
||||
|
||||
size_t c_return_identity(size_t n) { return n; }
|
||||
|
Loading…
Reference in New Issue
Block a user