summaryrefslogtreecommitdiff
path: root/codegen/load_with.rs
blob: 0e8e6725ad73591ed8f769aa261573f729663172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use opengl_registry::command::Command;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

pub fn create_load_with_function(commands: &[Command]) -> TokenStream
{
    let command_assignments = commands.iter().map(|command| {
        let command_name = format_ident!("{}", command.prototype().name());

        let command_name_str = command.prototype().name();

        quote! {
            functions::#command_name = get_proc_addr(#command_name_str);
        }
    });

    quote! {
        /// Loads OpenGL function pointers using the given function address retriever function.
        pub fn load_with<GetProcAddrFn>(get_proc_addr: GetProcAddrFn)
        where
            GetProcAddrFn: Fn(&str) -> unsafe extern "C" fn(),
        {
            unsafe {
                #(#command_assignments)*
            }
        }
    }
}