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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::collections::HashMap;
use std::string::String;

use implement::render::router;

pub struct ShaderCollection {
    _programs: HashMap< u64, ( router::ShaderType, i64 ) >,
    _id_to_descrip: HashMap< u64, String >,
    _descrip_to_id: HashMap< String, u64 >,
}


impl Default for ShaderCollection {
    fn default() -> ShaderCollection {
        ShaderCollection {
            _programs: HashMap::new(),
            _id_to_descrip: HashMap::new(),
            _descrip_to_id: HashMap::new(),
        }
    }
}
    
impl ShaderCollection {
    pub fn put( & mut self, id: u64, shader_type: router::ShaderType, internal_handle: i64, descrip: String ) -> Result< (), & 'static str > {
        match self._programs.insert( id, ( shader_type, internal_handle ) ) {
            None => (),
            Some( ( shader_type, old_handle ) ) => {
                router::delete_shader_program( old_handle, shader_type )?;
                println!( "removed old shader program( {} ).", old_handle );
            }
        }
        self._id_to_descrip.insert( id, descrip.clone() );
        self._descrip_to_id.insert( descrip, id );
        Ok( () )
    }
    pub fn clear( & mut self ) -> Result< (), & 'static str > {
        for ( _, &( ref shader_type, ref handle ) ) in self._programs.iter() {
            router::delete_shader_program( *handle, (*shader_type).clone() )?;
        }
        self._id_to_descrip.clear();
        self._descrip_to_id.clear();
        Ok( () )
    }
    pub fn remove( & mut self, id: u64 ) -> Result< (), & 'static str > {
        match self._programs.remove( &id ) {
            Some( ( shader_type, handle ) ) => {
                router::delete_shader_program( handle, shader_type )?;
                if let Some( descrip ) = self._id_to_descrip.remove( &id ) {
                    self._descrip_to_id.remove( &descrip );
                };
                ()
            },
            None => (),
        }
        Ok( () )
    }
    pub fn get( & mut self, id: u64 ) -> Option< i64 > {
        match self._programs.get( &id ) {
            Some( &( ref _shader_type, ref handle ) ) => {
                return Some( *handle )
            },
            None => return None
        }
    }
}