Skip to content

Fix/clippy #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions generator/templates/audio_context.tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn constructor(ctx: CallContext) -> Result<JsUndefined> {
}
}
Either::B(js_number) => {
let latency = js_number.get_double()? as f64;
let latency = js_number.get_double()?;
AudioContextLatencyCategory::Custom(latency)
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ fn get_current_time(ctx: CallContext) -> Result<JsNumber> {
let napi_obj = ctx.env.unwrap::<${d.napiName(d.node)}>(&js_this)?;
let obj = napi_obj.unwrap();

let current_time = obj.current_time() as f64;
let current_time = obj.current_time();
ctx.env.create_double(current_time)
}

Expand Down Expand Up @@ -384,7 +384,7 @@ fn get_base_latency(ctx: CallContext) -> Result<JsNumber> {
let napi_obj = ctx.env.unwrap::<${d.napiName(d.node)}>(&js_this)?;
let obj = napi_obj.unwrap();

let base_latency = obj.base_latency() as f64;
let base_latency = obj.base_latency();
ctx.env.create_double(base_latency)
}

Expand All @@ -394,7 +394,7 @@ fn get_output_latency(ctx: CallContext) -> Result<JsNumber> {
let napi_obj = ctx.env.unwrap::<${d.napiName(d.node)}>(&js_this)?;
let obj = napi_obj.unwrap();

let output_latency = obj.output_latency() as f64;
let output_latency = obj.output_latency();
ctx.env.create_double(output_latency)
}

Expand All @@ -409,10 +409,8 @@ fn set_sink_id(ctx: CallContext) -> Result<JsUndefined> {

let res = obj.set_sink_id_sync(sink_id);

if res.is_err() {
return Err(napi::Error::from_reason(
res.unwrap_err().to_string(),
));
if let Err(msg) = res {
return Err(napi::Error::from_reason(msg.to_string()));
}

ctx.env.get_undefined()
Expand Down
28 changes: 26 additions & 2 deletions generator/templates/audio_nodes.tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn constructor(ctx: CallContext) -> Result<JsUndefined> {
return `
let some_${simple_slug}_js = options_js.get::<&str, JsNumber>("${m.name}")?;
let ${slug} = if let Some(${simple_slug}_js) = some_${simple_slug}_js {
${simple_slug}_js.get_double()? as f64
${simple_slug}_js.get_double()?
} else {
${m.required ? `return Err(napi::Error::from_reason(
"Parameter ${d.name(m)} is required".to_string(),
Expand Down Expand Up @@ -525,7 +525,31 @@ fn get_${d.slug(attr)}(ctx: CallContext) -> Result<JsBoolean> {
`;
break;
case 'float':
return `
#[js_function(0)]
fn get_${d.slug(attr)}(ctx: CallContext) -> Result<JsNumber> {
let js_this = ctx.this_unchecked::<JsObject>();
let napi_node = ctx.env.unwrap::<${d.napiName(d.node)}>(&js_this)?;
let node = napi_node.unwrap();

let value = node.${d.slug(attr, true)}();
ctx.env.create_double(value as f64)
}
`;
break;
case 'double':
return `
#[js_function(0)]
fn get_${d.slug(attr)}(ctx: CallContext) -> Result<JsNumber> {
let js_this = ctx.this_unchecked::<JsObject>();
let napi_node = ctx.env.unwrap::<${d.napiName(d.node)}>(&js_this)?;
let node = napi_node.unwrap();

let value = node.${d.slug(attr, true)}();
ctx.env.create_double(value)
}
`;
break;
case 'unsigned long':
return `
#[js_function(0)]
Expand Down Expand Up @@ -662,7 +686,7 @@ fn set_${d.slug(attr)}(ctx: CallContext) -> Result<JsUndefined> {
let napi_node = ctx.env.unwrap::<${d.napiName(d.node)}>(&js_this)?;
let node = napi_node.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f64;
let value = ctx.get::<JsNumber>(0)?.get_double()?;
node.set_${d.slug(attr)}(value);

ctx.env.get_undefined()
Expand Down
16 changes: 8 additions & 8 deletions generator/templates/audio_param.tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn linear_ramp_to_value_at_time(ctx: CallContext) -> Result<JsUndefined> {
let obj = napi_obj.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f32;
let end_time = ctx.get::<JsNumber>(1)?.get_double()? as f64;
let end_time = ctx.get::<JsNumber>(1)?.get_double()?;
obj.linear_ramp_to_value_at_time(value, end_time);

ctx.env.get_undefined()
Expand All @@ -93,7 +93,7 @@ fn exponential_ramp_to_value_at_time(ctx: CallContext) -> Result<JsUndefined> {
let obj = napi_obj.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f32;
let end_time = ctx.get::<JsNumber>(1)?.get_double()? as f64;
let end_time = ctx.get::<JsNumber>(1)?.get_double()?;
obj.exponential_ramp_to_value_at_time(value, end_time);

ctx.env.get_undefined()
Expand All @@ -108,8 +108,8 @@ fn set_value_curve_at_time(ctx: CallContext) -> Result<JsUndefined> {
let mut typed_array_values = ctx.get::<JsTypedArray>(0)?.into_value()?;
let values: &mut [f32] = typed_array_values.as_mut();

let start_time = ctx.get::<JsNumber>(1)?.get_double()? as f64;
let duration = ctx.get::<JsNumber>(2)?.get_double()? as f64;
let start_time = ctx.get::<JsNumber>(1)?.get_double()?;
let duration = ctx.get::<JsNumber>(2)?.get_double()?;
obj.set_value_curve_at_time(values, start_time, duration);

ctx.env.get_undefined()
Expand All @@ -122,8 +122,8 @@ fn set_target_at_time(ctx: CallContext) -> Result<JsUndefined> {
let obj = napi_obj.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f32;
let start_time = ctx.get::<JsNumber>(1)?.get_double()? as f64;
let time_constant = ctx.get::<JsNumber>(2)?.get_double()? as f64;
let start_time = ctx.get::<JsNumber>(1)?.get_double()?;
let time_constant = ctx.get::<JsNumber>(2)?.get_double()?;
obj.set_target_at_time(value, start_time, time_constant);

ctx.env.get_undefined()
Expand All @@ -135,7 +135,7 @@ fn cancel_scheduled_values(ctx: CallContext) -> Result<JsUndefined> {
let napi_obj = ctx.env.unwrap::<NapiAudioParam>(&js_this)?;
let obj = napi_obj.unwrap();

let cancel_time = ctx.get::<JsNumber>(0)?.get_double()? as f64;
let cancel_time = ctx.get::<JsNumber>(0)?.get_double()?;
obj.cancel_scheduled_values(cancel_time);

ctx.env.get_undefined()
Expand All @@ -147,7 +147,7 @@ fn cancel_and_hold_at_time(ctx: CallContext) -> Result<JsUndefined> {
let napi_obj = ctx.env.unwrap::<NapiAudioParam>(&js_this)?;
let obj = napi_obj.unwrap();

let cancel_time = ctx.get::<JsNumber>(0)?.get_double()? as f64;
let cancel_time = ctx.get::<JsNumber>(0)?.get_double()?;
obj.cancel_and_hold_at_time(cancel_time);

ctx.env.get_undefined()
Expand Down
18 changes: 9 additions & 9 deletions src/analyser_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ fn constructor(ctx: CallContext) -> Result<JsUndefined> {

let some_max_decibels_js = options_js.get::<&str, JsNumber>("maxDecibels")?;
let max_decibels = if let Some(max_decibels_js) = some_max_decibels_js {
max_decibels_js.get_double()? as f64
max_decibels_js.get_double()?
} else {
-30.
};

let some_min_decibels_js = options_js.get::<&str, JsNumber>("minDecibels")?;
let min_decibels = if let Some(min_decibels_js) = some_min_decibels_js {
min_decibels_js.get_double()? as f64
min_decibels_js.get_double()?
} else {
-100.
};
Expand All @@ -142,7 +142,7 @@ fn constructor(ctx: CallContext) -> Result<JsUndefined> {
options_js.get::<&str, JsNumber>("smoothingTimeConstant")?;
let smoothing_time_constant =
if let Some(smoothing_time_constant_js) = some_smoothing_time_constant_js {
smoothing_time_constant_js.get_double()? as f64
smoothing_time_constant_js.get_double()?
} else {
0.8
};
Expand Down Expand Up @@ -368,7 +368,7 @@ fn get_min_decibels(ctx: CallContext) -> Result<JsNumber> {
let node = napi_node.unwrap();

let value = node.min_decibels();
ctx.env.create_double(value as f64)
ctx.env.create_double(value)
}

#[js_function(0)]
Expand All @@ -378,7 +378,7 @@ fn get_max_decibels(ctx: CallContext) -> Result<JsNumber> {
let node = napi_node.unwrap();

let value = node.max_decibels();
ctx.env.create_double(value as f64)
ctx.env.create_double(value)
}

#[js_function(0)]
Expand All @@ -388,7 +388,7 @@ fn get_smoothing_time_constant(ctx: CallContext) -> Result<JsNumber> {
let node = napi_node.unwrap();

let value = node.smoothing_time_constant();
ctx.env.create_double(value as f64)
ctx.env.create_double(value)
}

// -------------------------------------------------
Expand All @@ -413,7 +413,7 @@ fn set_min_decibels(ctx: CallContext) -> Result<JsUndefined> {
let napi_node = ctx.env.unwrap::<NapiAnalyserNode>(&js_this)?;
let node = napi_node.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f64;
let value = ctx.get::<JsNumber>(0)?.get_double()?;
node.set_min_decibels(value);

ctx.env.get_undefined()
Expand All @@ -425,7 +425,7 @@ fn set_max_decibels(ctx: CallContext) -> Result<JsUndefined> {
let napi_node = ctx.env.unwrap::<NapiAnalyserNode>(&js_this)?;
let node = napi_node.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f64;
let value = ctx.get::<JsNumber>(0)?.get_double()?;
node.set_max_decibels(value);

ctx.env.get_undefined()
Expand All @@ -437,7 +437,7 @@ fn set_smoothing_time_constant(ctx: CallContext) -> Result<JsUndefined> {
let napi_node = ctx.env.unwrap::<NapiAnalyserNode>(&js_this)?;
let node = napi_node.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f64;
let value = ctx.get::<JsNumber>(0)?.get_double()?;
node.set_smoothing_time_constant(value);

ctx.env.get_undefined()
Expand Down
4 changes: 2 additions & 2 deletions src/audio_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use web_audio_api::{AudioBuffer, AudioBufferOptions};

// helper convert [f32] to [u8]
// https://users.rust-lang.org/t/vec-f32-to-u8/21522/7
fn to_byte_slice<'a>(floats: &'a [f32]) -> &'a [u8] {
fn to_byte_slice(floats: &[f32]) -> &[u8] {
unsafe { std::slice::from_raw_parts(floats.as_ptr() as *const _, floats.len() * 4) }
}

Expand Down Expand Up @@ -158,7 +158,7 @@ fn duration(ctx: CallContext) -> Result<JsNumber> {
let obj = napi_obj.unwrap();

let duration = obj.duration();
ctx.env.create_double(duration as f64)
ctx.env.create_double(duration)
}

#[js_function]
Expand Down
12 changes: 6 additions & 6 deletions src/audio_buffer_source_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ fn constructor(ctx: CallContext) -> Result<JsUndefined> {

let some_loop_end_js = options_js.get::<&str, JsNumber>("loopEnd")?;
let loop_end = if let Some(loop_end_js) = some_loop_end_js {
loop_end_js.get_double()? as f64
loop_end_js.get_double()?
} else {
0.
};

let some_loop_start_js = options_js.get::<&str, JsNumber>("loopStart")?;
let loop_start = if let Some(loop_start_js) = some_loop_start_js {
loop_start_js.get_double()? as f64
loop_start_js.get_double()?
} else {
0.
};
Expand Down Expand Up @@ -393,7 +393,7 @@ fn get_loop_start(ctx: CallContext) -> Result<JsNumber> {
let node = napi_node.unwrap();

let value = node.loop_start();
ctx.env.create_double(value as f64)
ctx.env.create_double(value)
}

#[js_function(0)]
Expand All @@ -403,7 +403,7 @@ fn get_loop_end(ctx: CallContext) -> Result<JsNumber> {
let node = napi_node.unwrap();

let value = node.loop_end();
ctx.env.create_double(value as f64)
ctx.env.create_double(value)
}

// -------------------------------------------------
Expand Down Expand Up @@ -444,7 +444,7 @@ fn set_loop_start(ctx: CallContext) -> Result<JsUndefined> {
let napi_node = ctx.env.unwrap::<NapiAudioBufferSourceNode>(&js_this)?;
let node = napi_node.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f64;
let value = ctx.get::<JsNumber>(0)?.get_double()?;
node.set_loop_start(value);

ctx.env.get_undefined()
Expand All @@ -456,7 +456,7 @@ fn set_loop_end(ctx: CallContext) -> Result<JsUndefined> {
let napi_node = ctx.env.unwrap::<NapiAudioBufferSourceNode>(&js_this)?;
let node = napi_node.unwrap();

let value = ctx.get::<JsNumber>(0)?.get_double()? as f64;
let value = ctx.get::<JsNumber>(0)?.get_double()?;
node.set_loop_end(value);

ctx.env.get_undefined()
Expand Down
14 changes: 6 additions & 8 deletions src/audio_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn constructor(ctx: CallContext) -> Result<JsUndefined> {
}
}
Either::B(js_number) => {
let latency = js_number.get_double()? as f64;
let latency = js_number.get_double()?;
AudioContextLatencyCategory::Custom(latency)
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ fn get_current_time(ctx: CallContext) -> Result<JsNumber> {
let napi_obj = ctx.env.unwrap::<NapiAudioContext>(&js_this)?;
let obj = napi_obj.unwrap();

let current_time = obj.current_time() as f64;
let current_time = obj.current_time();
ctx.env.create_double(current_time)
}

Expand All @@ -192,10 +192,8 @@ fn get_listener(ctx: CallContext) -> Result<JsObject> {

// reproduce lazy instanciation strategy from rust crate
if js_this.has_named_property("__listener__").ok().unwrap() {
println!("reuse");
js_this.get_named_property("__listener__")
} else {
println!("wrap");
let store_ref: &mut napi::Ref<()> = ctx.env.get_instance_data()?.unwrap();
let store: JsObject = ctx.env.get_reference_value(store_ref)?;
let ctor: JsFunction = store.get_named_property("AudioListener")?;
Expand Down Expand Up @@ -557,7 +555,7 @@ fn get_base_latency(ctx: CallContext) -> Result<JsNumber> {
let napi_obj = ctx.env.unwrap::<NapiAudioContext>(&js_this)?;
let obj = napi_obj.unwrap();

let base_latency = obj.base_latency() as f64;
let base_latency = obj.base_latency();
ctx.env.create_double(base_latency)
}

Expand All @@ -567,7 +565,7 @@ fn get_output_latency(ctx: CallContext) -> Result<JsNumber> {
let napi_obj = ctx.env.unwrap::<NapiAudioContext>(&js_this)?;
let obj = napi_obj.unwrap();

let output_latency = obj.output_latency() as f64;
let output_latency = obj.output_latency();
ctx.env.create_double(output_latency)
}

Expand All @@ -582,8 +580,8 @@ fn set_sink_id(ctx: CallContext) -> Result<JsUndefined> {

let res = obj.set_sink_id_sync(sink_id);

if res.is_err() {
return Err(napi::Error::from_reason(res.unwrap_err().to_string()));
if let Err(msg) = res {
return Err(napi::Error::from_reason(msg.to_string()));
}

ctx.env.get_undefined()
Expand Down
Loading