Skip to content

Commit

Permalink
Actually implement zngur_dbg
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Oct 9, 2023
1 parent 799eacc commit 7c9b4dd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
9 changes: 9 additions & 0 deletions examples/memory_management/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,13 @@ int main() {
std::cout << "Checkpoint 30" << std::endl;
}
std::cout << "Checkpoint 31" << std::endl;
{
auto p1 = zngur_dbg(PrintOnDrop(rust::Str::from_char_star("dbg_A")));
std::cout << "Checkpoint 32" << std::endl;
auto p2 = zngur_dbg(std::move(p1));
std::cout << "Checkpoint 33" << std::endl;
zngur_dbg(p2);
std::cout << "Checkpoint 34" << std::endl;
}
std::cout << "Checkpoint 35" << std::endl;
}
1 change: 1 addition & 0 deletions examples/memory_management/main.zng
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod crate {

type PrintOnDrop {
#layout(size = 16, align = 8);
wellknown_traits(Debug);
constructor(&str);

fn clone(&self) -> PrintOnDrop;
Expand Down
16 changes: 16 additions & 0 deletions examples/memory_management/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Checkpoint 20
Checkpoint 21
PrintOnDrop(A) has been dropped
Checkpoint 22
thread '<unnamed>' panicked at 'consume_and_panic executed with value B', examples/memory_management/src/lib.rs:29:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
PrintOnDrop(B) has been dropped
PrintOnDrop(A) has been dropped
Checkpoint 24
Expand All @@ -56,4 +58,18 @@ Checkpoint 30
PrintOnDrop(elem1) has been dropped
PrintOnDrop(elem2) has been dropped
Checkpoint 31
[main.cpp:117] PrintOnDrop(rust::Str::from_char_star("dbg_A")) = PrintOnDrop(
"dbg_A",
)
Checkpoint 32
[main.cpp:119] std::move(p1) = PrintOnDrop(
"dbg_A",
)
Checkpoint 33
[main.cpp:121] p2 = PrintOnDrop(
"dbg_A",
)
Checkpoint 34
PrintOnDrop(dbg_A) has been dropped
Checkpoint 35
PrintOnDrop(C) has been dropped
2 changes: 1 addition & 1 deletion examples/memory_management/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod generated;

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

pub struct PrintOnDropPair {
Expand Down
2 changes: 1 addition & 1 deletion examples/rayon/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main() {
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) {
rust::Send>>::make_box([&](rust::Ref<uint64_t> x) {
return is_prime(*x);
});
std::cout << "Sum = " << slice.par_iter().sum() << std::endl;
Expand Down
19 changes: 11 additions & 8 deletions zngur-generator/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ private:
friend void ::rust::__zngur_internal_check_init<{ty}>(const {ty}& t);
friend void ::rust::__zngur_internal_assume_init<{ty}>({ty}& t);
friend void ::rust::__zngur_internal_assume_deinit<{ty}>({ty}& t);
friend void ::rust::zngur_pretty_print<{ty}>({ty}& t);
friend void ::rust::zngur_pretty_print<{ty}>({ty} const& t);
"#,
ty = self.ty,
)?;
Expand Down Expand Up @@ -1263,7 +1263,7 @@ auto data_as_impl = &args;
r#"
namespace rust {{
template<>
inline void zngur_pretty_print<{ty}>({ty}& t) {{
inline void zngur_pretty_print<{ty}>({ty} const& t) {{
::rust::__zngur_internal_check_init<{ty}>(t);
{pretty_print}(&t.data[0]);
}}
Expand Down Expand Up @@ -1360,11 +1360,7 @@ impl CppFile {
"#;
}
state.text += r#"
#define zngur_dbg(x) \
{ \
::std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] " << #x << " = "; \
::rust::zngur_pretty_print(x); \
}
#define zngur_dbg(x) (::rust::zngur_dbg_impl(__FILE__, __LINE__, #x, x))
namespace rust {
template<typename T>
Expand Down Expand Up @@ -1430,12 +1426,19 @@ namespace rust {
using Unit = Tuple<>;
template<typename T>
void zngur_pretty_print(T&) {}
void zngur_pretty_print(const T&);
class Inherent;
template<typename Type, typename Trait = Inherent>
class Impl;
template<typename T>
T&& zngur_dbg_impl(const char* file_name, int line_number, const char* exp, T&& input) {
::std::cerr << "[" << file_name << ":" << line_number << "] " << exp << " = ";
zngur_pretty_print<typename ::std::remove_reference<T>::type>(input);
return ::std::forward<T>(input);
}
"#;
for ty in [8, 16, 32, 64]
.into_iter()
Expand Down

0 comments on commit 7c9b4dd

Please sign in to comment.