Skip to content

boost::scoped_ptr demo #524

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

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 scoped_ptr_demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "scoped-ptr-demo"
version = "0.0.0"
edition = "2018"
links = "scoped-ptr-demo"
publish = false

[dependencies]
cxx = "1.0"

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

[workspace]
6 changes: 6 additions & 0 deletions scoped_ptr_demo/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
cxx_build::bridge("src/main.rs")
.file("src/demo.cc")
.flag_if_supported("-std=c++17")
.compile("scoped-ptr-demo");
}
12 changes: 12 additions & 0 deletions scoped_ptr_demo/include/demo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <boost/scoped_ptr.hpp>

struct Class {
~Class();
void print() const;
int32_t x;
};

using ScopedClass = boost::scoped_ptr<Class>;

void run();
10 changes: 10 additions & 0 deletions scoped_ptr_demo/src/demo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "scoped-ptr-demo/include/demo.h"
#include "scoped-ptr-demo/src/main.rs.h"
#include <iostream>

Class::~Class() { std::cout << x << "::~Class " << std::endl; }
void Class::print() const { std::cout << x << "::print" << std::endl; }

void run() {
recv(boost::scoped_ptr{new Class{9}}, boost::scoped_ptr{new Class{1}});
}
44 changes: 44 additions & 0 deletions scoped_ptr_demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use cxx::ExternType;
use std::ops::Deref;

#[repr(transparent)]
pub struct ScopedPtr<T>(*mut T);

impl<T> Deref for ScopedPtr<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { &*self.0 }
}
}

unsafe impl ExternType for ScopedPtr<ffi::Class> {
type Id = cxx::type_id!("ScopedClass");
type Kind = cxx::kind::Opaque;
}

#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("scoped-ptr-demo/include/demo.h");

type ScopedClass = crate::ScopedPtr<Class>;

fn run();

type Class;
fn print(self: &Class);
}

extern "Rust" {
fn recv(a: &ScopedClass, b: &ScopedClass);
}
}

fn recv(a: &ffi::ScopedClass, b: &ffi::ScopedClass) {
a.print();
b.print();
}

fn main() {
ffi::run();
}