@@ -465,53 +465,65 @@ pub enum BufferMapAsyncResult<T> {
465465 Error ,
466466}
467467
468- struct BufferMapReadAsyncUserData < F : FnOnce ( BufferMapAsyncResult < & [ u8 ] > ) > {
468+ struct BufferMapReadAsyncUserData < T , F >
469+ where F : FnOnce ( BufferMapAsyncResult < & [ T ] > ) {
469470 size : u32 ,
470471 callback : F ,
472+ phantom : std:: marker:: PhantomData < T > ,
471473}
472474
473- struct BufferMapWriteAsyncUserData < F : FnOnce ( BufferMapAsyncResult < & mut [ u8 ] > ) > {
475+ struct BufferMapWriteAsyncUserData < T , F >
476+ where F : FnOnce ( BufferMapAsyncResult < & mut [ T ] > ) {
474477 size : u32 ,
475478 callback : F ,
479+ phantom : std:: marker:: PhantomData < T > ,
476480}
477481
478482impl Buffer {
479483 pub fn set_sub_data ( & self , offset : u32 , data : & [ u8 ] ) {
480484 wgn:: wgpu_buffer_set_sub_data ( self . id , offset, data. len ( ) as u32 , data. as_ptr ( ) ) ;
481485 }
482486
483- pub fn map_read_async < F > ( & self , start : u32 , size : u32 , callback : F )
484- where F : FnOnce ( BufferMapAsyncResult < & [ u8 ] > ) {
485- extern "C" fn buffer_map_read_callback_wrapper < F > ( status : wgn:: BufferMapAsyncStatus , data : * const u8 , userdata : * mut u8 )
486- where F : FnOnce ( BufferMapAsyncResult < & [ u8 ] > ) {
487- let userdata = unsafe { Box :: from_raw ( userdata as * mut BufferMapReadAsyncUserData < F > ) } ;
488- let data = unsafe { slice:: from_raw_parts ( data, userdata. size as usize ) } ;
487+ pub fn map_read_async < T , F > ( & self , start : u32 , size : u32 , callback : F )
488+ where T : ' static + Copy , F : FnOnce ( BufferMapAsyncResult < & [ T ] > ) {
489+ let type_size = std:: mem:: size_of :: < T > ( ) as u32 ;
490+ assert_ne ! ( type_size, 0 ) ;
491+ assert_eq ! ( size % type_size, 0 ) ;
492+
493+ extern "C" fn buffer_map_read_callback_wrapper < T , F > ( status : wgn:: BufferMapAsyncStatus , data : * const u8 , userdata : * mut u8 )
494+ where F : FnOnce ( BufferMapAsyncResult < & [ T ] > ) {
495+ let userdata = unsafe { Box :: from_raw ( userdata as * mut BufferMapReadAsyncUserData < T , F > ) } ;
496+ let data = unsafe { slice:: from_raw_parts ( data as * const T , userdata. size as usize / std:: mem:: size_of :: < T > ( ) ) } ;
489497 if let wgn:: BufferMapAsyncStatus :: Success = status {
490- ( userdata. callback ) ( BufferMapAsyncResult :: Success ( data) ) ;
498+ ( userdata. callback ) ( BufferMapAsyncResult :: Success :: < & [ T ] > ( data) ) ;
491499 } else {
492500 ( userdata. callback ) ( BufferMapAsyncResult :: Error ) ;
493501 }
494502 }
495503
496- let userdata = Box :: new ( BufferMapReadAsyncUserData { size, callback} ) ;
497- wgn:: wgpu_buffer_map_read_async ( self . id , start, size, buffer_map_read_callback_wrapper :: < F > , Box :: into_raw ( userdata) as * mut u8 ) ;
504+ let userdata = Box :: new ( BufferMapReadAsyncUserData { size, callback, phantom : std :: marker :: PhantomData } ) ;
505+ wgn:: wgpu_buffer_map_read_async ( self . id , start, size, buffer_map_read_callback_wrapper :: < T , F > , Box :: into_raw ( userdata) as * mut u8 ) ;
498506 }
499507
500- pub fn map_write_async < F > ( & self , start : u32 , size : u32 , callback : F )
501- where F : FnOnce ( BufferMapAsyncResult < & mut [ u8 ] > ) {
502- extern "C" fn buffer_map_write_callback_wrapper < F > ( status : wgn:: BufferMapAsyncStatus , data : * mut u8 , userdata : * mut u8 )
503- where F : FnOnce ( BufferMapAsyncResult < & mut [ u8 ] > ) {
504- let userdata = unsafe { Box :: from_raw ( userdata as * mut BufferMapWriteAsyncUserData < F > ) } ;
505- let data = unsafe { slice:: from_raw_parts_mut ( data, userdata. size as usize ) } ;
508+ pub fn map_write_async < T , F > ( & self , start : u32 , size : u32 , callback : F )
509+ where T : ' static + Copy , F : FnOnce ( BufferMapAsyncResult < & mut [ T ] > ) {
510+ let type_size = std:: mem:: size_of :: < T > ( ) as u32 ;
511+ assert_ne ! ( type_size, 0 ) ;
512+ assert_eq ! ( size % type_size, 0 ) ;
513+
514+ extern "C" fn buffer_map_write_callback_wrapper < T , F > ( status : wgn:: BufferMapAsyncStatus , data : * mut u8 , userdata : * mut u8 )
515+ where F : FnOnce ( BufferMapAsyncResult < & mut [ T ] > ) {
516+ let userdata = unsafe { Box :: from_raw ( userdata as * mut BufferMapWriteAsyncUserData < T , F > ) } ;
517+ let data = unsafe { slice:: from_raw_parts_mut ( data as * mut T , userdata. size as usize / std:: mem:: size_of :: < T > ( ) ) } ;
506518 if let wgn:: BufferMapAsyncStatus :: Success = status {
507- ( userdata. callback ) ( BufferMapAsyncResult :: Success ( data) ) ;
519+ ( userdata. callback ) ( BufferMapAsyncResult :: Success :: < & mut [ T ] > ( data) ) ;
508520 } else {
509521 ( userdata. callback ) ( BufferMapAsyncResult :: Error ) ;
510522 }
511523 }
512524
513- let userdata = Box :: new ( BufferMapWriteAsyncUserData { size, callback} ) ;
514- wgn:: wgpu_buffer_map_write_async ( self . id , start, size, buffer_map_write_callback_wrapper :: < F > , Box :: into_raw ( userdata) as * mut u8 ) ;
525+ let userdata = Box :: new ( BufferMapWriteAsyncUserData { size, callback, phantom : std :: marker :: PhantomData } ) ;
526+ wgn:: wgpu_buffer_map_write_async ( self . id , start, size, buffer_map_write_callback_wrapper :: < T , F > , Box :: into_raw ( userdata) as * mut u8 ) ;
515527 }
516528
517529 pub fn unmap ( & self ) {
0 commit comments