Skip to content

Commit

Permalink
Add std::initializer_list constructor for rust::Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 30, 2020
1 parent 861e476 commit 265cdf6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gen/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions gen/src/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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>");
}
Expand All @@ -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>");
}
Expand Down
9 changes: 9 additions & 0 deletions include/cxx.h
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -238,6 +240,7 @@ class Vec final {
using value_type = T;

Vec() noexcept;
Vec(std::initializer_list<T>) noexcept;
Vec(Vec &&) noexcept;
~Vec() noexcept;

Expand Down Expand Up @@ -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) noexcept : 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();
Expand Down
5 changes: 3 additions & 2 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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; }
Expand Down

0 comments on commit 265cdf6

Please sign in to comment.