Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo Box<dyn Trait> #672

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions dyntrait/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "demo-dyntrait"
version = "0.0.0"
edition = "2018"
publish = false

[dependencies]
anyhow = "1.0"
cxx = "1.0"

[build-dependencies]
cxx-build = "1.0"

[workspace]
5 changes: 5 additions & 0 deletions dyntrait/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
cxx_build::bridge("src/main.rs")
.file("src/main.cc")
.compile("demo-dyntrait");
}
18 changes: 18 additions & 0 deletions dyntrait/include/mydata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <array>
#include <cstdint>
#include <type_traits>

class BoxDynMyData {
public:
BoxDynMyData(BoxDynMyData &&) noexcept;
~BoxDynMyData() noexcept;
using IsRelocatable = std::true_type;

void traitfn() const noexcept;

private:
std::array<std::uintptr_t, 2> repr;
};

using PtrBoxDynMyData = BoxDynMyData*;
20 changes: 20 additions & 0 deletions dyntrait/src/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "demo-dyntrait/src/main.rs"

BoxDynMyData::BoxDynMyData(BoxDynMyData &&other) noexcept : repr(other.repr) {
other.repr = {0, 0};
}

BoxDynMyData::~BoxDynMyData() noexcept {
if (repr != std::array<std::uintptr_t, 2>{0, 0}) {
dyn_mydata_drop_in_place(this);
}
}

void BoxDynMyData::traitfn() const noexcept {
dyn_mydata_traitfn(*this);
}

int main() {
auto mydata = read_data();
mydata.traitfn();
}
54 changes: 54 additions & 0 deletions dyntrait/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![no_main]

use anyhow::Result;
use cxx::ExternType;

pub trait MyData {
fn traitfn(&self);
}

unsafe impl ExternType for Box<dyn MyData> {
type Id = cxx::type_id!("BoxDynMyData");
type Kind = cxx::kind::Trivial;
}

#[repr(transparent)]
pub struct PtrBoxDynMyData(*mut Box<dyn MyData>);
unsafe impl ExternType for PtrBoxDynMyData {
type Id = cxx::type_id!("PtrBoxDynMyData");
type Kind = cxx::kind::Trivial;
}

#[cxx::bridge]
mod ffi {
extern "C++" {
include!("demo-dyntrait/include/mydata.h");
type BoxDynMyData = Box<dyn crate::MyData>;
type PtrBoxDynMyData = crate::PtrBoxDynMyData;
}

extern "Rust" {
fn dyn_mydata_traitfn(mydata: &BoxDynMyData);
unsafe fn dyn_mydata_drop_in_place(ptr: PtrBoxDynMyData);

fn read_data() -> Result<BoxDynMyData>;
}
}

fn dyn_mydata_traitfn(mydata: &Box<dyn MyData>) {
(**mydata).traitfn();
}

unsafe fn dyn_mydata_drop_in_place(ptr: PtrBoxDynMyData) {
std::ptr::drop_in_place(ptr.0);
}

fn read_data() -> Result<Box<dyn MyData>> {
struct Implementation(usize);
impl MyData for Implementation {
fn traitfn(&self) {
println!("it worked! {}", self.0);
}
}
Ok(Box::new(Implementation(9)))
}