Skip to content

Commit

Permalink
Merge pull request #203 from cunarist/add-sample-crate-examples
Browse files Browse the repository at this point in the history
Add sample crate examples
  • Loading branch information
temeddix authored Oct 25, 2023
2 parents cbcd969 + 1fb4e53 commit 8e707ea
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion documentation/docs/writing-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ You might be used to `println!` macro in Rust. However, using that macro isn't a
When writing Rust code in the `hub` crate, you can simply print your debug message with the `debug_print!` macro provided by this framework like below. Once you use this macro, Flutter will take care of the rest.

```rust
crate::debug_print!("My object is {:?}", my_object);
crate::debug_print!("My object is {my_object:?}");
```

`debug_print!` is also better than `println!` because it only works in debug mode, resulting in a smaller and cleaner release binary.
2 changes: 1 addition & 1 deletion flutter_ffi_plugin/example/native/hub/src/bridge/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub fn start_rust_logic() {
{
#[cfg(debug_assertions)]
std::panic::set_hook(Box::new(|panic_info| {
crate::debug_print!("A panic occurred in Rust.\n{}", panic_info);
crate::debug_print!("A panic occurred in Rust.\n{panic_info}");
}));
IS_MAIN_STARTED.with(move |ref_cell| {
let is_started = *ref_cell.borrow();
Expand Down
18 changes: 14 additions & 4 deletions flutter_ffi_plugin/example/native/hub/src/sample_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub async fn handle_counter_number(rust_request: RustRequest) -> RustResponse {
// Decode raw bytes into a Rust message object.
let message_bytes = rust_request.message.unwrap();
let request_message = ReadRequest::decode(message_bytes.as_slice()).unwrap();
crate::debug_print!("{}", request_message.letter);
let letter = request_message.letter;
crate::debug_print!("{letter}");

// Perform a simple calculation.
let after_value: i32 = sample_crate::add_seven(request_message.before_number);
Expand Down Expand Up @@ -126,7 +127,12 @@ pub async fn stream_mandelbrot() {
}

pub async fn run_debug_tests() {
if !SHOULD_DEMONSTRATE {
#[cfg(debug_assertions)]
const IS_DEBUG_MODE: bool = true;
#[cfg(not(debug_assertions))]
const IS_DEBUG_MODE: bool = false;

if !SHOULD_DEMONSTRATE || !IS_DEBUG_MODE {
return;
}

Expand All @@ -135,12 +141,16 @@ pub async fn run_debug_tests() {

// Get the current time.
let current_time = sample_crate::get_current_time();
crate::debug_print!("System time: {}", current_time);
crate::debug_print!("System time: {current_time}");

// Fetch data from a web API.
let web_response = sample_crate::fetch_from_web_api().await;
crate::debug_print!("Response from a web API: {web_response}");

// Use a crate that accesses operating system APIs.
let option = sample_crate::get_hardward_id();
if let Some(hwid) = option {
crate::debug_print!("Hardware ID: {}", hwid);
crate::debug_print!("Hardware ID: {hwid}");
} else {
crate::debug_print!("Hardware ID is not available on this platform.");
}
Expand Down
4 changes: 2 additions & 2 deletions flutter_ffi_plugin/example/native/hub/src/web_alias.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The web has many restrictions due to its sandboxed environment
//! which prevents the use of
//! threads, atomics, time, file IO, network IO,
//! threads, time, file IO, network IO,
//! and many other native functionalities.
//! Consequently, certain features are missing from various crates
//! including Rust's `std` due to these limitations.
Expand Down Expand Up @@ -35,7 +35,7 @@
// allows millions of concurrent tasks to run at the same time.
// On the web, concurrent tasks are executed
// in JavaScript's single-threaded event loop.
// Crate `wasm_bindgen_futures` has the ability
// Crate `async_wasm_task` has the ability
// to convert Rust `Future`s into JavaScript `Promise`s.

#[cfg(not(target_family = "wasm"))]
Expand Down
1 change: 1 addition & 0 deletions flutter_ffi_plugin/example/native/sample_crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ machineid-rs = "1.2.4"
num = "0.4"
image = "0.24.3"
chrono = "0.4.31"
reqwest = "0.11.22"
11 changes: 11 additions & 0 deletions flutter_ffi_plugin/example/native/sample_crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ use chrono::{offset, DateTime};
pub fn get_current_time() -> DateTime<offset::Local> {
offset::Local::now()
}

// `reqwest` supports all platforms, including web.

pub async fn fetch_from_web_api() -> String {
reqwest::get("https://jsonplaceholder.typicode.com/todos/1")
.await
.expect("Could not get the response from the example web API.")
.text()
.await
.expect("Could not read body from the web response.")
}

0 comments on commit 8e707ea

Please sign in to comment.