Skip to content

Commit

Permalink
mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
kuecks committed Oct 18, 2023
1 parent a6ca025 commit 6cd254f
Show file tree
Hide file tree
Showing 42 changed files with 1,525 additions and 27 deletions.
2 changes: 1 addition & 1 deletion demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
repository = "https://github.com/dtolnay/cxx"

[dependencies]
cxx = "1.0"
cxx = {version="1.0", features=["c++17"]}

[build-dependencies]
cxx-build = "1.0"
2 changes: 1 addition & 1 deletion demo/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() {
cxx_build::bridge("src/main.rs")
.file("src/blobstore.cc")
.flag_if_supported("-std=c++14")
.flag("-std=c++17")
.compile("cxxbridge-demo");

println!("cargo:rerun-if-changed=src/main.rs");
Expand Down
5 changes: 5 additions & 0 deletions demo/include/blobstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ class BlobstoreClient {

std::unique_ptr<BlobstoreClient> new_blobstore_client();

::rust::Option<::rust::Box<BlobMetadata>> c_take_option_box(::rust::Option<::rust::Box<BlobMetadata>>);
::rust::Option<const BlobMetadata*> c_take_option_ref(::rust::Option<const BlobMetadata *>);
::rust::Option<BlobMetadata*> c_take_option_ref_mut(::rust::Option<BlobMetadata *>);
void c_take_rust_ref_option_opaque(rust::Option<const MultiBuf*>);

} // namespace blobstore
} // namespace org
38 changes: 38 additions & 0 deletions demo/src/blobstore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#include "demo/src/main.rs.h"
#include <algorithm>
#include <functional>
#include <vector>
#include <set>
#include <string>
#include <unordered_map>
#include <iostream>

namespace org {
namespace blobstore {
Expand All @@ -27,6 +29,19 @@ BlobstoreClient::BlobstoreClient() : impl(new class BlobstoreClient::impl) {}
// Upload a new blob and return a blobid that serves as a handle to the blob.
uint64_t BlobstoreClient::put(MultiBuf &buf) const {
std::string contents;
BlobMetadata metadata{};
metadata.tags.push_back("cpp");
metadata.size = 1;
auto rrob = r_return_option_box();
std::cout << "rrob.inner.empty: " << rrob.inner.empty << std::endl;
std::cout << "rrob.has_value(): " << rrob.has_value() << std::endl;
std::cout << "rrob.value().size: " << rrob.value()->size << std::endl;
std::cout << "rrob.value().tags[0]: " << rrob.value()->tags[0] << std::endl;

// auto rror = r_return_option_ref(metadata);
// std::cout << "rror.has_value(): " << rror.has_value() << std::endl;
// std::cout << "rror.value().size: " << rror.value()->size << std::endl;
// std::cout << "rror.value().tags[0]: " << rror.value()->tags[0] << std::endl;

// Traverse the caller's chunk iterator.
//
Expand Down Expand Up @@ -67,5 +82,28 @@ std::unique_ptr<BlobstoreClient> new_blobstore_client() {
return std::make_unique<BlobstoreClient>();
}

rust::Option<rust::Box<BlobMetadata>>c_take_option_box(rust::Option<rust::Box<BlobMetadata>> r) {
std::cout << "r.inner.empty: " << r.inner.empty << std::endl;
std::cout << "r.has_value(): " << r.has_value() << std::endl;
std::cout << "r.value().size: " << r.value()->size << std::endl;
std::cout << "r.value().tags[0]: " << r.value()->tags[0] << std::endl;
return r;
}

rust::Option<const BlobMetadata *> c_take_option_ref(rust::Option<const BlobMetadata *> r) {
std::cout << "r.has_value(): " << r.has_value() << std::endl;
std::cout << "r.value().size: " << r.value()->size << std::endl;
std::cout << "r.value().tags[0]: " << r.value()->tags[0] << std::endl;
return r;
}

rust::Option<BlobMetadata *> c_take_option_ref_mut(rust::Option<BlobMetadata *> r) {
std::cout << "r.has_value(): " << r.has_value() << std::endl;
std::cout << "r.value().size: " << r.value()->size << std::endl;
std::cout << "r.value().tags[0]: " << r.value()->tags[0] << std::endl;
r.value()->tags[0] = "cpp";
return r;
}

} // namespace blobstore
} // namespace org
77 changes: 77 additions & 0 deletions demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use cxx::{CxxVector, UniquePtr};
use std::pin::Pin;

#[cxx::bridge(namespace = "org::blobstore")]
mod ffi {
// Shared structs with fields visible to both languages.
Expand All @@ -10,7 +13,20 @@ mod ffi {
extern "Rust" {
type MultiBuf;

fn r_return_str() -> &'static str;
fn next_chunk(buf: &mut MultiBuf) -> &[u8];
fn r_return_option_box() -> Option<Box<BlobMetadata>>;
fn r_return_option_ref() -> Option<&'static u8>;
fn r_take_option_box(o: Option<Box<BlobMetadata>>);
unsafe fn r_take_option_pin<'a>(
o: Option<Pin<&'a mut BlobMetadata>>,
) -> Option<Pin<&'a mut BlobMetadata>>;

fn r_take_vec(v: Vec<BlobMetadata>);
fn r_return_vec() -> Vec<BlobMetadata>;
fn r_return_box() -> Box<BlobMetadata>;

fn foo(_: UniquePtr<CxxVector<BlobMetadata>>) -> u32;
}

// C++ types and signatures exposed to Rust.
Expand All @@ -23,6 +39,10 @@ mod ffi {
fn put(&self, parts: &mut MultiBuf) -> u64;
fn tag(&self, blobid: u64, tag: &str);
fn metadata(&self, blobid: u64) -> BlobMetadata;

fn c_take_option_box(rust: Option<Box<BlobMetadata>>) -> Option<Box<BlobMetadata>>;
fn c_take_option_ref(rust: Option<&BlobMetadata>) -> Option<&BlobMetadata>;
fn c_take_option_ref_mut(rust: Option<&mut BlobMetadata>) -> Option<&mut BlobMetadata>;
}
}

Expand All @@ -41,7 +61,64 @@ pub fn next_chunk(buf: &mut MultiBuf) -> &[u8] {
next.map_or(&[], Vec::as_slice)
}

pub fn r_return_str() -> &'static str {
"rust"
}

fn r_return_option_box() -> Option<Box<ffi::BlobMetadata>> {
Some(Box::new(ffi::BlobMetadata {
size: 42,
tags: vec!["rust".to_owned()],
}))
}

fn r_return_option_ref() -> Option<&'static u8> {
Some(&42)
}

fn r_take_option_box(o: Option<Box<ffi::BlobMetadata>>) {
let _ = o;
}

unsafe fn r_take_option_pin<'a>(
o: Option<Pin<&'a mut ffi::BlobMetadata>>,
) -> Option<Pin<&'a mut ffi::BlobMetadata>> {
o
}

pub fn foo(cpp: UniquePtr<CxxVector<ffi::BlobMetadata>>) -> u32 {
cpp.len() as u32
}

fn r_take_vec(v: Vec<ffi::BlobMetadata>) {
let _ = v;
}

fn r_return_vec() -> Vec<ffi::BlobMetadata> {
Vec::new()
}

fn r_return_box() -> Box<ffi::BlobMetadata> {
Box::new(ffi::BlobMetadata {
size: 0,
tags: vec![],
})
}

fn main() {
let opt = ffi::c_take_option_box(Some(Box::new(ffi::BlobMetadata {
size: 42,
tags: vec!["rust".to_owned()],
})));
println!("opt.unwrap().tags: {:?}", opt.unwrap().tags);
let mut metadata = ffi::BlobMetadata {
size: 42,
tags: vec!["rust".to_owned()],
};
let opt = ffi::c_take_option_ref(Some(&metadata));
println!("opt.unwrap().tags: {:?}", opt.unwrap().tags);
let opt = ffi::c_take_option_ref_mut(Some(&mut metadata));
println!("opt.unwrap().tags: {:?}", opt.unwrap().tags);
let client = ffi::new_blobstore_client();

// Upload a blob.
Expand Down
1 change: 1 addition & 0 deletions gen/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) struct Builtins<'a> {
pub rust_slice: bool,
pub rust_box: bool,
pub rust_vec: bool,
pub rust_option: bool,
pub rust_fn: bool,
pub rust_isize: bool,
pub opaque: bool,
Expand Down
Loading

0 comments on commit 6cd254f

Please sign in to comment.