Skip to content

Commit

Permalink
Add some using to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Oct 14, 2023
1 parent ee81d52 commit 87654d4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
a.out
.vscode
compile_commands.json
41 changes: 19 additions & 22 deletions examples/osmium/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ using namespace rust::crate;
using namespace std;
template <typename T, typename E>
using Result = rust::std::result::Result<T, E>;
template <typename T> using Option = rust::std::option::Option<T>;
template <typename T> using Ref = rust::Ref<T>;
using rust::std::string::String;

Result<Reader, rust::std::string::String>
rust::exported_functions::new_reader(Flags f) {
Result<Reader, 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));
return Result<Reader, String>::Ok(move(o));
} catch (const exception &ex) {
return Result<Reader, rust::std::string::String>::Err(
return Result<Reader, String>::Err(
rust::Str::from_char_star(ex.what()).to_string());
}
}
Expand All @@ -36,7 +38,7 @@ class RustHandler : public osmium::handler::Handler {
RustHandler(BendHandler &&inner) : inner(std::move(inner)) {}
};

rust::Tuple<> rust::Impl<Reader>::apply(rust::Ref<Reader> reader,
rust::Tuple<> rust::Impl<Reader>::apply(Ref<Reader> reader,
BendHandler handler) {
using IndexType =
osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type,
Expand All @@ -49,43 +51,38 @@ rust::Tuple<> rust::Impl<Reader>::apply(rust::Ref<Reader> reader,
return {};
}

rust::Ref<WayNodeList> rust::Impl<Way>::nodes(rust::Ref<Way> self) {
Ref<WayNodeList> rust::Impl<Way>::nodes(Ref<Way> self) {
return self.cpp().nodes();
}

rust::Ref<TagList> rust::Impl<Way>::tags(rust::Ref<Way> self) {
return self.cpp().tags();
}
Ref<TagList> rust::Impl<Way>::tags(Ref<Way> self) { return self.cpp().tags(); }

rust::std::option::Option<rust::Ref<rust::Str>>
rust::Impl<TagList>::get_value_by_key(rust::Ref<TagList> self,
rust::Ref<rust::Str> key) {
Option<Ref<rust::Str>>
rust::Impl<TagList>::get_value_by_key(Ref<TagList> self, Ref<rust::Str> key) {
string cpp_key{(const char *)key.as_ptr(), key.len()};
auto value = self.cpp().get_value_by_key(cpp_key.c_str());
if (value == nullptr) {
return rust::std::option::Option<rust::Ref<rust::Str>>::None();
return Option<Ref<rust::Str>>::None();
}
return rust::std::option::Option<rust::Ref<rust::Str>>::Some(
rust::Str::from_char_star(value));
return Option<Ref<rust::Str>>::Some(rust::Str::from_char_star(value));
}

size_t rust::Impl<WayNodeList>::len(rust::Ref<WayNodeList> self) {
size_t rust::Impl<WayNodeList>::len(Ref<WayNodeList> self) {
return self.cpp().size();
}

rust::Ref<Node>
rust::Impl<WayNodeList, rust::std::ops::Index<size_t, Node>>::index(
rust::Ref<WayNodeList> self, size_t i) {
Ref<Node> rust::Impl<WayNodeList, rust::std::ops::Index<size_t, Node>>::index(
Ref<WayNodeList> self, size_t i) {
return self.cpp()[i];
}

double rust::Impl<Node>::distance(rust::Ref<Node> self, rust::Ref<Node> other) {
double rust::Impl<Node>::distance(Ref<Node> self, Ref<Node> other) {
return osmium::geom::haversine::distance(self.cpp().location(),
other.cpp().location());
}

rust::std::string::String rust::Impl<Node>::href(rust::Ref<Node> self) {
auto s = rust::std::string::String::new_();
String rust::Impl<Node>::href(Ref<Node> self) {
auto s = String::new_();
s.push_str(rust::Str::from_char_star("https://www.openstreetmap.org/node/"));
s.push_str(rust::Str::from_char_star(to_string(self.cpp().ref()).c_str()));
return s;
Expand Down
15 changes: 10 additions & 5 deletions examples/rayon/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

#include "./generated.h"

template <typename T> using Box = rust::Box<T>;
template <typename T> using Ref = rust::Ref<T>;
template <typename... T> using Dyn = rust::Dyn<T...>;
template <typename... T> using Fn = rust::Fn<T...>;
using rust::Bool;
using rust::Send;
using rust::Sync;

bool is_prime(uint64_t v) {
if (v < 2)
return 0;
Expand All @@ -21,11 +29,8 @@ int main() {
std::vector<uint64_t> v(10000000);
std::iota(v.begin(), v.end(), 1);
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>>::make_box([&](rust::Ref<uint64_t> x) {
return is_prime(*x);
});
auto f = Box<Dyn<Fn<Ref<uint64_t>, Bool>, Sync, Send>>::make_box(
[&](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
13 changes: 7 additions & 6 deletions examples/tutorial_cpp/impls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@

using namespace rust::crate;

template <typename T> using Ref = rust::Ref<T>;
template <typename T> using RefMut = rust::RefMut<T>;

Inventory rust::Impl<Inventory>::new_empty(uint32_t space) {
return Inventory(
rust::ZngurCppOpaqueOwnedObject::build<cpp_inventory::Inventory>(space));
}

rust::Unit rust::Impl<Inventory>::add_banana(rust::RefMut<Inventory> self,
rust::Unit rust::Impl<Inventory>::add_banana(RefMut<Inventory> self,
uint32_t count) {
self.cpp().add_banana(count);
return {};
}

rust::Unit rust::Impl<Inventory>::add_item(rust::RefMut<Inventory> self,
Item item) {
rust::Unit rust::Impl<Inventory>::add_item(RefMut<Inventory> self, Item item) {
self.cpp().add_item(item.cpp());
return {};
}

Item rust::Impl<Item>::new_(rust::Ref<rust::Str> name, uint32_t size) {
Item rust::Impl<Item>::new_(Ref<rust::Str> name, uint32_t size) {
return Item(rust::ZngurCppOpaqueOwnedObject::build<cpp_inventory::Item>(
cpp_inventory::Item{
.name = ::std::string(reinterpret_cast<const char *>(name.as_ptr()),
Expand All @@ -29,8 +31,7 @@ Item rust::Impl<Item>::new_(rust::Ref<rust::Str> name, uint32_t size) {
}

rust::std::fmt::Result rust::Impl<Inventory, rust::std::fmt::Debug>::fmt(
rust::Ref<::rust::crate::Inventory> self,
rust::RefMut<::rust::std::fmt::Formatter> f) {
Ref<Inventory> self, RefMut<rust::std::fmt::Formatter> f) {
::std::string result = "Inventory { remaining_space: ";
result += ::std::to_string(self.cpp().remaining_space);
result += ", items: [";
Expand Down

0 comments on commit 87654d4

Please sign in to comment.