Skip to content

Commit

Permalink
Make all GL functions unsafe
Browse files Browse the repository at this point in the history
Closes #185
  • Loading branch information
emberian committed Nov 5, 2014
1 parent 0e8fa29 commit 3b9357f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 46 deletions.
20 changes: 2 additions & 18 deletions src/gl_generator/generators/global_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,7 @@ fn write_fns(ecx: &ExtCtxt, registry: &Registry) -> Vec<P<ast::Item>> {
None => "".to_string()
};

ecx.parse_item(if c.is_safe {
format!(
"#[allow(non_snake_case)] #[allow(unused_variables)] #[allow(dead_code)]
#[inline] #[unstable] {doc} pub fn {name}({params}) -> {return_suffix} {{ \
unsafe {{ \
__gl_imports::mem::transmute::<_, extern \"system\" fn({types}) -> {return_suffix}>\
(storage::{name}.f)({idents}) \
}} \
}}",
name = c.proto.ident,
doc = doc,
params = super::gen_parameters(ecx, c).into_iter().map(|p| p.to_source()).collect::<Vec<String>>().connect(", "),
types = super::gen_parameters(ecx, c).into_iter().map(|p| p.ty.to_source()).collect::<Vec<String>>().connect(", "),
return_suffix = super::gen_return_type(ecx, c).to_source(),
idents = super::gen_parameters(ecx, c).into_iter().map(|p| p.pat.to_source()).collect::<Vec<String>>().connect(", "),
)
} else {
ecx.parse_item(
format!(
"#[allow(non_snake_case)] #[allow(unused_variables)] #[allow(dead_code)]
#[inline] #[unstable] {doc} pub unsafe fn {name}({typed_params}) -> {return_suffix} {{ \
Expand All @@ -136,7 +120,7 @@ fn write_fns(ecx: &ExtCtxt, registry: &Registry) -> Vec<P<ast::Item>> {
return_suffix = super::gen_return_type(ecx, c).to_source(),
idents = super::gen_parameters(ecx, c).into_iter().map(|p| p.pat.to_source()).collect::<Vec<String>>().connect(", "),
)
})
)
}).collect()
}

Expand Down
39 changes: 11 additions & 28 deletions src/gl_generator/generators/struct_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,34 +220,17 @@ fn write_impl(ecx: &ExtCtxt, registry: &Registry, ns: &Ns) -> P<ast::Item> {
modules = registry.cmd_iter().map(|c| {
use syntax::ext::quote::rt::ToSource;

if c.is_safe {
format!(
"#[allow(non_snake_case)] #[allow(unused_variables)] #[allow(dead_code)]
#[inline] #[unstable] pub fn {name}(&self, {params}) -> {return_suffix} {{ \
unsafe {{ \
__gl_imports::mem::transmute::<_, extern \"system\" fn({types}) -> {return_suffix}>\
(self.{name}.f)({idents}) \
}} \
}}",
name = c.proto.ident,
params = super::gen_parameters(ecx, c).into_iter().map(|p| p.to_source()).collect::<Vec<String>>().connect(", "),
types = super::gen_parameters(ecx, c).into_iter().map(|p| p.ty.to_source()).collect::<Vec<String>>().connect(", "),
return_suffix = super::gen_return_type(ecx, c).to_source(),
idents = super::gen_parameters(ecx, c).into_iter().map(|p| p.pat.to_source()).collect::<Vec<String>>().connect(", "),
)
} else {
format!(
"#[allow(non_snake_case)] #[allow(unused_variables)] #[allow(dead_code)]
#[inline] #[unstable] pub unsafe fn {name}(&self, {typed_params}) -> {return_suffix} {{ \
__gl_imports::mem::transmute::<_, extern \"system\" fn({typed_params}) -> {return_suffix}>\
(self.{name}.f)({idents}) \
}}",
name = c.proto.ident,
typed_params = super::gen_parameters(ecx, c).into_iter().map(|p| p.to_source()).collect::<Vec<String>>().connect(", "),
return_suffix = super::gen_return_type(ecx, c).to_source(),
idents = super::gen_parameters(ecx, c).into_iter().map(|p| p.pat.to_source()).collect::<Vec<String>>().connect(", "),
)
}
format!(
"#[allow(non_snake_case)] #[allow(unused_variables)] #[allow(dead_code)]
#[inline] #[unstable] pub unsafe fn {name}(&self, {typed_params}) -> {return_suffix} {{ \
__gl_imports::mem::transmute::<_, extern \"system\" fn({typed_params}) -> {return_suffix}>\
(self.{name}.f)({idents}) \
}}",
name = c.proto.ident,
typed_params = super::gen_parameters(ecx, c).into_iter().map(|p| p.to_source()).collect::<Vec<String>>().connect(", "),
return_suffix = super::gen_return_type(ecx, c).to_source(),
idents = super::gen_parameters(ecx, c).into_iter().map(|p| p.pat.to_source()).collect::<Vec<String>>().connect(", "),
)
}).collect::<Vec<String>>().connect("\n")
))
}
3 changes: 3 additions & 0 deletions src/gl_generator/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ pub struct Binding {
pub struct Cmd {
pub proto: Binding,
pub params: Vec<Binding>,
/// True if this command doesn't take any pointers.
///
/// Unused by the built-in generators.
pub is_safe: bool,
pub alias: Option<String>,
pub vecequiv: Option<String>,
Expand Down

0 comments on commit 3b9357f

Please sign in to comment.