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

Implement Clone for CompileOptions #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 25 additions & 20 deletions shaderc-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ use std::any::Any;
use std::cell::RefCell;
use std::ffi::{CStr, CString};
use std::panic;
use std::rc::Rc;
use std::{error, fmt, ptr, result, slice, str};

/// Error.
Expand Down Expand Up @@ -648,7 +649,7 @@ impl Drop for Compiler {
pub type IncludeCallbackResult = result::Result<ResolvedInclude, String>;

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

/// An opaque object managing options to compilation.
pub struct CompileOptions<'a> {
Expand Down Expand Up @@ -703,23 +704,6 @@ impl<'a> CompileOptions<'a> {
}
}

/// Returns a copy of the given compilation options object.
///
/// A return of `None` indicates that there was an error copying
/// the underlying options object.
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> Option<CompileOptions> {
let p = unsafe { scs::shaderc_compile_options_clone(self.raw) };
if p.is_null() {
None
} else {
Some(CompileOptions {
raw: p,
include_callback_fn: None,
})
}
}

/// Sets the target enviroment to `env`, affecting which warnings or errors
/// will be issued.
///
Expand Down Expand Up @@ -787,7 +771,7 @@ impl<'a> CompileOptions<'a> {
{
use std::mem;

let f = Box::new(f);
let f = Rc::new(f);
let f_ptr = &*f as *const F;
self.include_callback_fn = Some(f as BoxedIncludeCallback<'a>);
unsafe {
Expand Down Expand Up @@ -1115,6 +1099,27 @@ impl<'a> CompileOptions<'a> {
}
}

impl<'a> Clone for CompileOptions<'a> {
fn clone(&self) -> Self {
let p = unsafe { scs::shaderc_compile_options_clone(self.raw) };

// This should never happen, outside of a heap allocation failure.
// `shaderc_compile_options_clone()` is documented as being identical to
// `shaderc_compile_options_init()` when passed a NULL ptr, and there
// are no other failure conditions (it is a trivial C++ copy
// constructor).
assert!(
!p.is_null(),
"shaderc_compile_options_clone() returned NULL"
);

CompileOptions {
raw: p,
include_callback_fn: self.include_callback_fn.clone(),
}
}
}

impl<'a> Drop for CompileOptions<'a> {
fn drop(&mut self) {
unsafe { scs::shaderc_compile_options_release(self.raw) }
Expand Down Expand Up @@ -1442,7 +1447,7 @@ void main() { my_ssbo.x = 1.0; }";
let c = Compiler::new().unwrap();
let mut options = CompileOptions::new().unwrap();
options.add_macro_definition("E", None);
let o = options.clone().unwrap();
let o = options.clone();
let result = c
.compile_into_spirv_assembly(
IFDEF_E,
Expand Down
Loading