Skip to content
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

Clippy fixes #90

Merged
merged 6 commits into from
Oct 25, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 25 additions & 15 deletions shaderc-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,16 @@ impl Drop for Compiler {
}
}

/// Include callback status.
pub type IncludeCallbackResult = result::Result<ResolvedInclude, String>;

type BoxedIncludeCallback<'a> =
Box<dyn Fn(&str, IncludeType, &str, usize) -> IncludeCallbackResult + 'a>;

/// An opaque object managing options to compilation.
pub struct CompileOptions<'a> {
raw: *mut scs::ShadercCompileOptions,
f: Option<
Box<dyn Fn(&str, IncludeType, &str, usize) -> result::Result<ResolvedInclude, String> + 'a>,
>,
include_callback_fn: Option<BoxedIncludeCallback<'a>>,
}

/// Identifies the type of include directive. `Relative` is for include directives of the form
Expand Down Expand Up @@ -687,7 +691,10 @@ impl<'a> CompileOptions<'a> {
if p.is_null() {
None
} else {
Some(CompileOptions { raw: p, f: None })
Some(CompileOptions {
raw: p,
include_callback_fn: None,
})
}
}

Expand All @@ -700,7 +707,10 @@ impl<'a> CompileOptions<'a> {
if p.is_null() {
None
} else {
Some(CompileOptions { raw: p, f: None })
Some(CompileOptions {
raw: p,
include_callback_fn: None,
})
}
}

Expand Down Expand Up @@ -767,17 +777,13 @@ impl<'a> CompileOptions<'a> {
/// tried again with `Standard`, which is similar to include directive behaviour in C.
pub fn set_include_callback<F>(&mut self, f: F)
where
F: Fn(&str, IncludeType, &str, usize) -> result::Result<ResolvedInclude, String> + 'a,
F: Fn(&str, IncludeType, &str, usize) -> IncludeCallbackResult + 'a,
{
use std::mem;

let f = Box::new(f);
let f_ptr = &*f as *const F;
self.f = Some(
f as Box<
dyn Fn(&str, IncludeType, &str, usize) -> result::Result<ResolvedInclude, String> + 'a,
>,
);
self.include_callback_fn = Some(f as BoxedIncludeCallback<'a>);
unsafe {
scs::shaderc_compile_options_set_include_callbacks(
self.raw,
Expand Down Expand Up @@ -806,7 +812,7 @@ impl<'a> CompileOptions<'a> {
include_depth: size_t,
) -> *mut scs::shaderc_include_result
where
F: Fn(&str, IncludeType, &str, usize) -> result::Result<ResolvedInclude, String> + 'a,
F: Fn(&str, IncludeType, &str, usize) -> IncludeCallbackResult + 'a,
{
let result = panic::catch_unwind(move || {
let f = unsafe { &*(user_data as *const F) };
Expand Down Expand Up @@ -904,9 +910,7 @@ impl<'a> CompileOptions<'a> {

/// Sets the resource `limit` to the given `value`.
pub fn set_limit(&mut self, limit: Limit, value: i32) {
unsafe {
scs::shaderc_compile_options_set_limit(self.raw, limit as i32, value as c_int)
}
unsafe { scs::shaderc_compile_options_set_limit(self.raw, limit as i32, value as c_int) }
}

/// Sets whether the compiler should automatically assign bindings to uniforms
Expand Down Expand Up @@ -1115,6 +1119,11 @@ impl CompilationArtifact {
unsafe { scs::shaderc_result_get_length(self.raw) }
}

/// Returns true if the compilation output data has a length of 0.
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns the compilation output data as a binary slice.
///
/// # Panics
Expand All @@ -1131,6 +1140,7 @@ impl CompilationArtifact {

unsafe {
let p = scs::shaderc_result_get_bytes(self.raw);
#[allow(clippy::cast_ptr_alignment)]
slice::from_raw_parts(p as *const u32, num_words)
}
}
Expand Down
18 changes: 7 additions & 11 deletions shaderc-sys/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ fn sdk_path() -> Option<PathBuf> {

let output = if let Ok(out) = Command::new("xcrun")
.args(&["--sdk", sdk, "--show-sdk-path"])
.output() {
out.stdout
.output()
{
out.stdout
} else {
return None;
};
Expand Down Expand Up @@ -157,7 +158,8 @@ fn main() {
// Debian, Ubuntu and their derivatives.
Some(debian_triple_path)
} else if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "x86_64"
&& Path::new("/usr/lib64/").exists() {
&& Path::new("/usr/lib64/").exists()
{
// Other distributions running on x86_64 usually use this path.
Some("/usr/lib64/".to_owned())
} else {
Expand Down Expand Up @@ -237,14 +239,8 @@ fn main() {
println!("cargo:warning=Found and linking system installed Glslang and SPIRV-Tools libraries.");
println!("cargo:rustc-link-lib={}=glslang", glslang);
println!("cargo:rustc-link-lib={}=SPIRV", spirv);
println!(
"cargo:rustc-link-lib={}=SPIRV-Tools",
spirv_tools
);
println!(
"cargo:rustc-link-lib={}=SPIRV-Tools-opt",
spirv_tools_opt
);
println!("cargo:rustc-link-lib={}=SPIRV-Tools", spirv_tools);
println!("cargo:rustc-link-lib={}=SPIRV-Tools-opt", spirv_tools_opt);
}
_ => {
println!(
Expand Down
12 changes: 3 additions & 9 deletions shaderc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,10 @@ extern "C" {
);
pub fn shaderc_compile_options_set_hlsl_functionality1(
options: *mut ShadercCompileOptions,
enable: bool
);
pub fn shaderc_compile_options_set_invert_y(
options: *mut ShadercCompileOptions,
enable: bool
);
pub fn shaderc_compile_options_set_nan_clamp(
options: *mut ShadercCompileOptions,
enable: bool
enable: bool,
);
pub fn shaderc_compile_options_set_invert_y(options: *mut ShadercCompileOptions, enable: bool);
pub fn shaderc_compile_options_set_nan_clamp(options: *mut ShadercCompileOptions, enable: bool);

pub fn shaderc_result_release(result: *mut ShadercCompilationResult);
pub fn shaderc_result_get_compilation_status(result: *const ShadercCompilationResult) -> i32;
Expand Down