Skip to content

Commit

Permalink
Fix g++ compile error
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Sep 14, 2023
1 parent ee21ee6 commit adf5813
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 113 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/memory_management/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generated.h
generated.rs
13 changes: 13 additions & 0 deletions examples/memory_management/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "example-memory_management"
version = "0.0.0"
edition.workspace = true
license.workspace = true
publish = false

[lib]
crate-type = ["staticlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
10 changes: 10 additions & 0 deletions examples/memory_management/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
a.out: main.cpp generated.h src/generated.rs src/lib.rs ../../target/release/libexample_memory_management.a
${CXX} main.cpp -g -L ../../target/release/ -l example_memory_management

../../target/release/libexample_memory_management.a: FORCE
cargo build --release

generated.h ./src/generated.rs: main.zng
cd ../../zngur-cli && cargo run g ../examples/memory_management/main.zng

FORCE: ;
69 changes: 69 additions & 0 deletions examples/memory_management/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <vector>

#include "./generated.h"

// Rust values are available in the `::rust` namespace from their absolute path
// in Rust
template <typename T> using Vec = ::rust::std::vec::Vec<T>;
template <typename T> using Option = ::rust::std::option::Option<T>;
template <typename T> using BoxDyn = ::rust::Box<::rust::Dyn<T>>;
using rust::crate::PrintOnDrop;

// You can implement Rust traits for your classes
template <typename T>
class VectorIterator : public rust::Impl<::rust::std::iter::Iterator<T>> {
std::vector<T> vec;
size_t pos;

public:
VectorIterator(std::vector<T> &&v) : vec(v), pos(0) {}
~VectorIterator() {
std::cout << "vector iterator has been destructed" << std::endl;
}

Option<T> next() override {
if (pos >= vec.size()) {
return Option<T>::None();
}
T value = vec[pos++];
// You can construct Rust enum with fields in C++
return Option<T>::Some(value);
}
};

int main() {
auto p1 = PrintOnDrop(::rust::Str::from_char_star("A"));
auto p2 = PrintOnDrop(::rust::Str::from_char_star("B"));
auto p3 = PrintOnDrop(::rust::Str::from_char_star("C"));
std::cout << "Checkpoint 1" << std::endl;
p2 = std::move(p1);
std::cout << "Checkpoint 2" << std::endl;
{
std::cout << "Checkpoint 3" << std::endl;
PrintOnDrop p{std::move(p2)};
std::cout << "Checkpoint 4" << std::endl;
}
{
std::vector<PrintOnDrop> vec1;
vec1.emplace_back(::rust::Str::from_char_star("cpp_V1"));
vec1.emplace_back(::rust::Str::from_char_star("cpp_V2"));
vec1.emplace_back(::rust::Str::from_char_star("cpp_V3"));
std::cout << "Checkpoint 5" << std::endl;
vec1.pop_back();
std::cout << "Checkpoint 6" << std::endl;
}
{
std::cout << "Checkpoint 7" << std::endl;
Vec<PrintOnDrop> vec2 = Vec<PrintOnDrop>::new_();
vec2.push(PrintOnDrop(::rust::Str::from_char_star("rust_V1")));
vec2.push(PrintOnDrop(::rust::Str::from_char_star("rust_V2")));
std::cout << "Checkpoint 8" << std::endl;
vec2.clone(); // Clone and drop immediately
std::cout << "Checkpoint 9" << std::endl;
std::cout << "Checkpoint 10" << std::endl;
}
std::cout << "Checkpoint 11" << std::endl;
std::cout << "Checkpoint 12" << std::endl;
std::cout << "Checkpoint 13" << std::endl;
}
75 changes: 75 additions & 0 deletions examples/memory_management/main.zng
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
type () {
layout(size = 0, align = 1);
wellknown_traits(Copy);
}

type Box<dyn Fn(i32) -> i32> {
layout(size = 16, align = 8);
}

type str {
wellknown_traits(?Sized);
}

mod crate {
type PrintOnDrop {
layout(size = 16, align = 8);
constructor(&str);
}
}

mod ::std {
type option::Option<i32> {
layout(size = 8, align = 4);
wellknown_traits(Copy);

constructor None;
constructor Some(i32);

fn unwrap(self) -> i32;
}

type iter::Map<::std::vec::IntoIter<i32>, Box<dyn Fn(i32) -> i32>> {
layout(size = 48, align = 8);

fn sum<i32>(self) -> i32;
}

mod vec {
type IntoIter<i32> {
layout(size = 32, align = 8);

fn sum<i32>(self) -> i32;
fn map<i32, Box<dyn Fn(i32) -> i32>>(self, Box<dyn Fn(i32) -> i32>)
-> ::std::iter::Map<::std::vec::IntoIter<i32>, Box<dyn Fn(i32) -> i32>>;
}

type Vec<i32> {
layout(size = 24, align = 8);
wellknown_traits(Debug);

fn new() -> Vec<i32>;
fn push(&mut self, i32);
fn clone(&self) -> Vec<i32>;
fn into_iter(self) -> ::std::vec::IntoIter<i32>;
}

type Vec<crate::PrintOnDrop> {
layout(size = 24, align = 8);

fn new() -> Vec<crate::PrintOnDrop>;
fn push(&mut self, crate::PrintOnDrop);
fn clone(&self) -> Vec<crate::PrintOnDrop>;
}
}

trait iter::Iterator::<Item = i32> {
fn next(&mut self) -> ::std::option::Option<i32>;
}
}

type Box<dyn ::std::iter::Iterator<Item = i32>> {
layout(size = 16, align = 8);

fn collect<::std::vec::Vec<i32>>(self) -> ::std::vec::Vec<i32>;
}
10 changes: 10 additions & 0 deletions examples/memory_management/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod generated;

#[derive(Clone)]
struct PrintOnDrop(&'static str);

impl Drop for PrintOnDrop {
fn drop(&mut self) {
println!("PrintOnDrop({}) has been dropped", self.0);
}
}
Loading

0 comments on commit adf5813

Please sign in to comment.