Add std::initializer_list constructor for rust::Vec

This commit is contained in:
David Tolnay 2020-11-29 23:57:42 -08:00
parent 861e476a66
commit f799b3781c
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
5 changed files with 26 additions and 2 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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>");
}

View File

@ -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();

View File

@ -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; }