Skip to content

Commit

Permalink
Cargo fmt src.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbuchan committed May 5, 2021
1 parent 1f8a52e commit 6fb3a33
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
13 changes: 5 additions & 8 deletions src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ pub struct BindingContext {

impl BindingContext {
fn new(active: ActiveBindingContext) -> Self {
Self { active: Some(active) }
Self {
active: Some(active),
}
}

pub fn args(&self) -> &[JSObject] {
match &self.active {
None => &[],
Some(active) => {
active.payload["args"].as_array().expect("Expected array")
}
Some(active) => active.payload["args"].as_array().expect("Expected array"),
}
}

Expand Down Expand Up @@ -439,10 +439,7 @@ pub fn bind(c: Arc<Chrome>, name: &str, f: BindingFunc) -> Result<(), JSError> {
eval(Arc::clone(&c), &script).to_result_of_jserror()
}

fn complete_binding(
context: ActiveBindingContext,
result: JSResult,
) {
fn complete_binding(context: ActiveBindingContext, result: JSResult) {
let (r, e) = match result {
Ok(x) => (x.to_string(), r#""""#.to_string()),
Err(e) => ("".to_string(), e.to_string()),
Expand Down
2 changes: 1 addition & 1 deletion src/chrome/devtools.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{BindingContext, Chrome, ActiveBindingContext, JSObject, JSResult};
use super::{ActiveBindingContext, BindingContext, Chrome, JSObject, JSResult};
use super::{PipeReader, PipeWriter};
use crossbeam_channel::{bounded, Sender};
use serde_json::json;
Expand Down
26 changes: 15 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
mod chrome;
#[cfg(target_family = "windows")]
use chrome::close_handle;
use chrome::{bind, bounds, close, eval, load, load_css, load_js, set_bounds, BindingContext, Chrome};
use chrome::{
bind, bounds, close, eval, load, load_css, load_js, set_bounds, BindingContext, Chrome,
};
pub use chrome::{Bounds, JSError, JSObject, JSResult, WindowState};
mod locate;
pub use locate::tinyfiledialogs as dialog;
Expand Down Expand Up @@ -216,13 +218,17 @@ impl UI {
F: Fn(&[JSObject]) -> JSResult + Sync + Send + 'static,
{
let f = Arc::new(f);
bind(self.chrome.clone(), name, Arc::new(move |context| {
let f = f.clone();
std::thread::spawn(move || {
let result = f(context.args());
context.complete(result);
});
}))
bind(
self.chrome.clone(),
name,
Arc::new(move |context| {
let f = f.clone();
std::thread::spawn(move || {
let result = f(context.args());
context.complete(result);
});
}),
)
}

/// Bind a rust function callable from JS that can complete asynchronously.
Expand All @@ -244,9 +250,7 @@ impl UI {
// Capture the callers runtime, as using tokio::spawn() inside the binding function
// will fail as the message processing loop does not have a runtime registered.
let runtime = tokio::runtime::Handle::try_current()
.map_err(|err| {
JSError::from(JSObject::String(err.to_string()))
})?;
.map_err(|err| JSError::from(JSObject::String(err.to_string())))?;

self.bind_async(name, move |context| {
// Create future outside the spawn, avoiding the async block capturing `f`, which
Expand Down

0 comments on commit 6fb3a33

Please sign in to comment.