Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Oct 5, 2023
1 parent 28f09b1 commit da3f055
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ template <typename T> using BoxDyn = ::rust::Box<::rust::Dyn<T>>;

// You can implement Rust traits for your classes
template <typename T>
class VectorIterator : public rust::Impl<::rust::std::iter::Iterator<T>> {
class VectorIterator : public ::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()) {
Expand All @@ -48,6 +51,13 @@ int main() {
Vec<int32_t>::push(s, 3);
// You can call Rust functions just like normal Rust.
std::cout << s.clone().into_iter().sum() << std::endl;
// You can catch Rust panics as C++ exceptions
try {
std::cout << "s[2] = " << *s.get(2).unwrap() << std::endl;
std::cout << "s[4] = " << *s.get(4).unwrap() << std::endl;
} catch (rust::Panic e) {
std::cout << "Rust panic happened" << std::endl;
}
int state = 0;
// You can convert a C++ lambda into a `Box<dyn Fn>` and friends.
auto f = BoxDyn<::rust::Fn<int32_t, int32_t>>::build([&](int32_t x) {
Expand Down Expand Up @@ -76,16 +86,21 @@ Output:
```
17
s[2] = 7
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', examples/simple/src/generated.rs:186:39
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
s[4] = Rust panic happened
hello 2 2
hello 5 7
hello 7 14
hello 3 17
34 17
[main.cpp:61] t = [
vector iterator has been destructed
[main.cpp:71] t = [
10,
20,
60,
]
```

See the `examples/simple` directory if you want to build and run it.
See the [`examples/simple`](https://github.com/HKalbasi/zngur/blob/main/examples/simple) if you want to build and run it.
2 changes: 1 addition & 1 deletion book/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,4 @@ zngur_dbg(v);
]
```
You can see the full code at [`examples/tutorial`](https://github.com/HKalbasi/zngur/blob/main/examples/tutorial/main.zng)
You can see the full code at [`examples/tutorial`](https://github.com/HKalbasi/zngur/blob/main/examples/tutorial)
21 changes: 19 additions & 2 deletions book/src/zngur.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ template <typename T> using BoxDyn = ::rust::Box<::rust::Dyn<T>>;

// You can implement Rust traits for your classes
template <typename T>
class VectorIterator : public rust::Impl<::rust::std::iter::Iterator<T>> {
class VectorIterator : public ::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()) {
Expand All @@ -74,6 +77,13 @@ int main() {
Vec<int32_t>::push(s, 3);
// You can call Rust functions just like normal Rust.
std::cout << s.clone().into_iter().sum() << std::endl;
// You can catch Rust panics as C++ exceptions
try {
std::cout << "s[2] = " << *s.get(2).unwrap() << std::endl;
std::cout << "s[4] = " << *s.get(4).unwrap() << std::endl;
} catch (rust::Panic e) {
std::cout << "Rust panic happened" << std::endl;
}
int state = 0;
// You can convert a C++ lambda into a `Box<dyn Fn>` and friends.
auto f = BoxDyn<::rust::Fn<int32_t, int32_t>>::build([&](int32_t x) {
Expand Down Expand Up @@ -102,14 +112,21 @@ Output:
```
17
s[2] = 7
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', examples/simple/src/generated.rs:186:39
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
s[4] = Rust panic happened
hello 2 2
hello 5 7
hello 7 14
hello 3 17
34 17
[main.cpp:61] t = [
vector iterator has been destructed
[main.cpp:71] t = [
10,
20,
60,
]
```

See the [`examples/simple`](https://github.com/HKalbasi/zngur/blob/main/examples/simple) if you want to build and run it.
2 changes: 1 addition & 1 deletion examples/simple/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ int main() {
// Then use it like a normal Rust value.
auto t = vec_as_iter.collect();
// Some utilities are also provided. For example, `zngur_dbg` is the
// equivalent of `dbg!` macros.
// equivalent of `dbg!` macro.
zngur_dbg(t);
}

0 comments on commit da3f055

Please sign in to comment.