File tree Expand file tree Collapse file tree 1 file changed +8
-9
lines changed
Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change @@ -106,15 +106,13 @@ impl Ctl {
106106 }
107107
108108 pub fn elem_list ( & self ) -> Result < ElemList > {
109- // populate an empty list to get the number of elements
110- let empty_list = elem_list_new ( 0 ) ?;
111- acheck ! ( snd_ctl_elem_list( self . 0 , empty_list. 0 ) ) ?;
112- let required_elements = empty_list. get_count ( ) ;
113-
114109 // obtain the list of all the elements now that we know how many there are
115- let full_list = elem_list_new ( required_elements) ?;
116- acheck ! ( snd_ctl_elem_list( self . 0 , full_list. 0 ) ) ?;
117- Ok ( full_list)
110+ let list = elem_list_new ( |list| {
111+ acheck ! ( snd_ctl_elem_list( self . 0 , list. 0 ) ) ?;
112+ Ok ( list. get_count ( ) )
113+ } ) ?;
114+ acheck ! ( snd_ctl_elem_list( self . 0 , list. 0 ) ) ?;
115+ Ok ( list)
118116 }
119117
120118 /// Note: According to alsa-lib documentation, you're also supposed to have functionality for
@@ -470,9 +468,10 @@ impl Drop for ElemList {
470468 }
471469}
472470
473- pub fn elem_list_new ( count : u32 ) -> Result < ElemList > {
471+ fn elem_list_new < F : FnOnce ( & ElemList ) -> Result < u32 > > ( f : F ) -> Result < ElemList > {
474472 let mut p = ptr:: null_mut ( ) ;
475473 let list = acheck ! ( snd_ctl_elem_list_malloc( & mut p) ) . map ( |_| ElemList ( p) ) ?;
474+ let count = f ( & list) ?;
476475 if count > 0 {
477476 acheck ! ( snd_ctl_elem_list_alloc_space( list. 0 , count) ) ?;
478477 }
You can’t perform that action at this time.
0 commit comments