Skip to content

Commit

Permalink
Fix namespace path of rust functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Sep 30, 2023
1 parent 35890ad commit f2f67e0
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 42 deletions.
4 changes: 2 additions & 2 deletions examples/memory_management/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ 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>>;
template <typename T> using RmDyn = ::rust::Ref<::rust::Dyn<T>>;
using crate::consume_and_panic;
using crate::consume_n_times;
using rust::crate::consume_and_panic;
using rust::crate::consume_n_times;
using rust::crate::PrintOnDrop;
using rust::crate::PrintOnDropConsumer;
using rust::crate::PrintOnDropPair;
Expand Down
12 changes: 6 additions & 6 deletions examples/rayon/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ bool is_prime(uint64_t v) {
int main() {
std::vector<uint64_t> v(10000000);
std::iota(v.begin(), v.end(), 1);
auto slice = ::std::slice::from_raw_parts(v.data(), v.size());
auto f = ::rust::Box<
::rust::Dyn<::rust::Fn<::rust::Ref<uint64_t>, ::rust::Bool>, ::rust::Sync,
::rust::Send>>::build([&](::rust::Ref<uint64_t> x) {
return is_prime(*x);
});
auto slice = rust::std::slice::from_raw_parts(v.data(), v.size());
auto f =
rust::Box<rust::Dyn<rust::Fn<rust::Ref<uint64_t>, rust::Bool>, rust::Sync,
rust::Send>>::build([&](rust::Ref<uint64_t> x) {
return is_prime(*x);
});
std::cout << "Sum = " << slice.par_iter().sum() << std::endl;
std::cout << "Count of primes = "
<< slice.par_iter().copied().filter(std::move(f)).count()
Expand Down
9 changes: 4 additions & 5 deletions examples/rustyline/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

int main() {
auto editor = ::rust::rustyline::DefaultEditor::new_().unwrap();
if (editor.load_history(::crate::rust_str((signed char *)"history.txt"))
.is_err()) {
if (editor.load_history(rust::Str::from_char_star("history.txt")).is_err()) {
std::cout << "No previous history." << std::endl;
}
while (true) {
auto r = editor.readline(::crate::rust_str((signed char *)">>> "));
auto r = editor.readline(rust::Str::from_char_star(">>> "));
if (r.is_err()) {
auto e = r.unwrap_err();
if (e.matches_Eof()) {
Expand All @@ -26,8 +25,8 @@ int main() {
auto s = r.as_ref().unwrap().as_str();
std::string cpp_s((char *)s.as_ptr(), s.len());
std::cout << "Line: " << cpp_s << std::endl;
editor.add_history_entry(::crate::rust_str((signed char *)cpp_s.c_str()));
editor.add_history_entry(rust::Str::from_char_star(cpp_s.c_str()));
}
}
editor.save_history(::crate::rust_str((signed char *)"history.txt"));
editor.save_history(rust::Str::from_char_star("history.txt"));
}
16 changes: 6 additions & 10 deletions examples/rustyline/main.zng
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type () {
wellknown_traits(Copy);
}

type ::core::primitive::str {
type str {
wellknown_traits(?Sized); // Unsized types don't need layout policy

fn as_ptr(&self) -> *const u8;
Expand All @@ -20,7 +20,7 @@ type bool {
mod ::std {
type string::String {
#only_by_ref; // String has stable layout, but we don't use it by value, so we can use this policy.
fn as_str(&self) -> &::core::primitive::str;
fn as_str(&self) -> &str;
}
}

Expand All @@ -33,10 +33,10 @@ mod ::rustyline {
#heap_allocated;

fn new() -> Result<DefaultEditor>;
fn readline(&mut self, &::core::primitive::str) -> Result<::std::string::String>;
fn load_history<::core::primitive::str>(&mut self, &::core::primitive::str) -> Result<()>;
fn add_history_entry<&::core::primitive::str>(&mut self, &::core::primitive::str) -> Result<bool>;
fn save_history<::core::primitive::str>(&mut self, &::core::primitive::str) -> Result<()>;
fn readline(&mut self, &str) -> Result<::std::string::String>;
fn load_history<str>(&mut self, &str) -> Result<()>;
fn add_history_entry<&str>(&mut self, &str) -> Result<bool>;
fn save_history<str>(&mut self, &str) -> Result<()>;
}

type error::ReadlineError {
Expand Down Expand Up @@ -78,7 +78,3 @@ mod ::rustyline {
fn is_err(&self) -> bool;
}
}

mod crate {
fn rust_str(*const i8) -> &::core::primitive::str;
}
7 changes: 0 additions & 7 deletions examples/rustyline/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
use std::ffi::CStr;

mod generated;

pub fn rust_str<'a>(c: *const i8) -> &'a str {
let x = unsafe { CStr::from_ptr(c) };
unsafe { core::str::from_utf8_unchecked(x.to_bytes()) }
}
11 changes: 11 additions & 0 deletions zngur-generator/src/cpp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
collections::HashMap,
fmt::{Display, Write},
iter,
};

use iter_tools::Itertools;
Expand Down Expand Up @@ -38,6 +39,16 @@ impl CppPath {
fn need_header(&self) -> bool {
self.0.first().map(|x| x.as_str()) == Some("rust") && self.0 != ["rust", "Unit"]
}

pub(crate) fn from_rust_path(path: &[String]) -> CppPath {
CppPath(
iter::once("rust")
.chain(path.iter().map(|x| x.as_str()))
.map(cpp_handle_keyword)
.map(|x| x.to_owned())
.collect(),
)
}
}

impl<const N: usize> From<[&str; N]> for CppPath {
Expand Down
2 changes: 1 addition & 1 deletion zngur-generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl ZngurGenerator {
false,
);
cpp_file.fn_defs.push(CppFnDefinition {
name: CppPath(func.path.path),
name: CppPath::from_rust_path(&func.path.path),
sig: CppFnSig {
rust_link_name,
inputs: func.inputs.into_iter().map(|x| x.into_cpp()).collect(),
Expand Down
14 changes: 3 additions & 11 deletions zngur-generator/src/rust.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{fmt::Write, iter};
use std::fmt::Write;

use iter_tools::Itertools;

use crate::{
cpp::{
cpp_handle_keyword, CppLayoutPolicy, CppPath, CppTraitDefinition, CppTraitMethod, CppType,
},
cpp::{CppLayoutPolicy, CppPath, CppTraitDefinition, CppTraitMethod, CppType},
ZngurTrait, ZngurWellknownTrait, ZngurWellknownTraitData,
};

Expand All @@ -24,13 +22,7 @@ impl IntoCpp for RustPathAndGenerics {
} = self;
let named_generics = named_generics.iter().sorted_by_key(|x| &x.0).map(|x| &x.1);
CppType {
path: CppPath(
iter::once("rust")
.chain(path.iter().map(|x| x.as_str()))
.map(cpp_handle_keyword)
.map(|x| x.to_owned())
.collect(),
),
path: CppPath::from_rust_path(path),
generic_args: generics
.iter()
.chain(named_generics)
Expand Down

0 comments on commit f2f67e0

Please sign in to comment.