Skip to content

Commit

Permalink
Implement Clone for CompileOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
simonask committed Feb 17, 2024
1 parent 6e621ad commit 2466e40
Showing 1 changed file with 25 additions and 20 deletions.
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

0 comments on commit 2466e40

Please sign in to comment.