Skip to content

Commit

Permalink
Add README for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Oct 10, 2023
1 parent 43b997e commit 8acf58e
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/cxx_demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This example tries to replicate [the CXX demo](https://github.com/dtolnay/cxx/tree/master/demo) using Zngur. It tries to keep it as close
as possible to the original CXX example, so it doesn't use Zngur features that may make the code easier, more readable, and more
performant. To see an example that uses all Zngur features in C++ to Rust codes, go to s2 example.
performant. To see an example that uses all Zngur features in C++ to Rust codes, go to the osmium example.

To run this example:

Expand Down
10 changes: 10 additions & 0 deletions examples/memory_management/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Example: Memory management

Explains the corner cases of memory management by code. The output is available at `out.txt`.

To run this example:

```
make
./a.out
```
10 changes: 10 additions & 0 deletions examples/osmium/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Example: Osmium

This example contains a Rusty wrapper around osmium, a C++ library for working with the open street map. It also contains
a simple usage of that library inside the Rust code.

To run this example:

```
cargo run
```
20 changes: 14 additions & 6 deletions examples/osmium/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@

using namespace rust::crate;
using namespace std;
template <typename T, typename E>
using Result = rust::std::result::Result<T, E>;

Reader rust::exported_functions::new_blob_store_client(Flags f) {
Reader o(rust::ZngurCppOpaqueOwnedObject::build<osmium::io::Reader>(
"map.osm", static_cast<osmium::osm_entity_bits::type>(f.bits())));
return o;
Result<Reader, rust::std::string::String>
rust::exported_functions::new_reader(Flags f) {
try {
Reader o(rust::ZngurCppOpaqueOwnedObject::build<osmium::io::Reader>(
"map.osm", static_cast<osmium::osm_entity_bits::type>(f.bits())));
return Result<Reader, rust::std::string::String>::Ok(move(o));
} catch (const exception &ex) {
return Result<Reader, rust::std::string::String>::Err(
rust::Str::from_char_star(ex.what()).to_string());
}
}

class RustHandler : public osmium::handler::Handler {
Expand All @@ -28,8 +36,8 @@ class RustHandler : public osmium::handler::Handler {
RustHandler(BendHandler &&inner) : inner(std::move(inner)) {}
};

rust::Tuple<> rust::exported_functions::apply(rust::Ref<Reader> reader,
BendHandler handler) {
rust::Tuple<> rust::Impl<Reader>::apply(rust::Ref<Reader> reader,
BendHandler handler) {
using IndexType =
osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type,
osmium::Location>;
Expand Down
15 changes: 13 additions & 2 deletions examples/osmium/main.zng
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type str {

fn as_ptr(&self) -> *const u8;
fn len(&self) -> usize;
fn to_string(&self) -> ::std::string::String;
}

type ::std::option::Option<&str> {
Expand All @@ -42,6 +43,13 @@ type crate::Reader {
#cpp_value "0" "::osmium::io::Reader";
}

type ::std::result::Result<crate::Reader, ::std::string::String> {
#layout(size = 24, align = 8);

constructor Ok(crate::Reader);
constructor Err(::std::string::String);
}

type crate::Way {
#cpp_ref "::osmium::Way";
}
Expand Down Expand Up @@ -71,8 +79,11 @@ type crate::Flags {
}

extern "C++" {
fn new_blob_store_client(crate::Flags) -> crate::Reader;
fn apply(&crate::Reader, crate::BendHandler);
fn new_reader(crate::Flags) -> ::std::result::Result<crate::Reader, ::std::string::String>;

impl crate::Reader {
fn apply(&self, crate::BendHandler);
}

impl crate::Way {
fn nodes(&self) -> &crate::WayNodeList;
Expand Down
4 changes: 2 additions & 2 deletions examples/osmium/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ impl Handler for BendHandler {

fn main() {
let f = Flags::way | Flags::node;
let reader = generated::new_blob_store_client(f);
generated::apply(&reader, BendHandler { count: 0 });
let reader = generated::new_reader(f).unwrap();
reader.apply(BendHandler { count: 0 });
}
2 changes: 1 addition & 1 deletion examples/rayon/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Example: Rayon

Calculates the number of prime numbers in `1..10000000` with multiple cores using [rayon](https://github.com/rayon-rs/rayon).
Calculates the sum and the number of prime numbers in `1..10000000` with multiple cores using [rayon](https://github.com/rayon-rs/rayon).

To run this example:

Expand Down
10 changes: 10 additions & 0 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Example: Simple

A simple example, used as a demo in the main README file.

To run this example:

```
make
./a.out
```
10 changes: 10 additions & 0 deletions examples/tutorial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Example: Tutorial

Full code of the [Tutorial](https://hkalbasi.github.io/zngur/tutorial.html) in the Zngur book.

To run this example:

```
make
./a.out
```

0 comments on commit 8acf58e

Please sign in to comment.