88//!
99//! async fn open_db() {
1010//! // install opfs-sahpool persistent vfs and set as default vfs
11- //! install_opfs_sahpool(&OpfsSAHPoolCfg::default(), true)
11+ //! install_opfs_sahpool::<ffi::WasmOsCallback> (&OpfsSAHPoolCfg::default(), true)
1212//! .await
1313//! .unwrap();
1414//!
3232//! [`opfs-explorer`](https://chromewebstore.google.com/detail/opfs-explorer/acndjpgkpaclldomagafnognkcgjignd)
3333//! plugin to browse files.
3434
35- use sqlite_wasm_rs:: {
36- utils:: {
37- check_import_db, register_vfs, registered_vfs, sqlite3_file, sqlite3_filename, sqlite3_vfs,
38- sqlite3_vfs_register, sqlite3_vfs_unregister, ImportDbError , OsCallback , RegisterVfsError ,
39- SQLiteIoMethods , SQLiteVfs , SQLiteVfsFile , VfsAppData , VfsError , VfsFile , VfsResult ,
40- VfsStore ,
35+ use rsqlite_vfs:: {
36+ check_import_db,
37+ ffi:: {
38+ sqlite3_file, sqlite3_filename, sqlite3_vfs, sqlite3_vfs_register, sqlite3_vfs_unregister,
39+ SQLITE_CANTOPEN , SQLITE_ERROR , SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN , SQLITE_IOERR ,
40+ SQLITE_IOERR_DELETE , SQLITE_OK , SQLITE_OPEN_DELETEONCLOSE , SQLITE_OPEN_MAIN_DB ,
41+ SQLITE_OPEN_MAIN_JOURNAL , SQLITE_OPEN_SUPER_JOURNAL , SQLITE_OPEN_WAL ,
4142 } ,
42- WasmOsCallback , SQLITE_CANTOPEN , SQLITE_ERROR , SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN ,
43- SQLITE_IOERR , SQLITE_IOERR_DELETE , SQLITE_OK , SQLITE_OPEN_DELETEONCLOSE , SQLITE_OPEN_MAIN_DB ,
44- SQLITE_OPEN_MAIN_JOURNAL , SQLITE_OPEN_SUPER_JOURNAL , SQLITE_OPEN_WAL ,
43+ register_vfs, registered_vfs, ImportDbError , OsCallback , RegisterVfsError , SQLiteIoMethods ,
44+ SQLiteVfs , SQLiteVfsFile , VfsAppData , VfsError , VfsFile , VfsResult , VfsStore ,
4545} ;
46- use std:: cell:: { Cell , RefCell } ;
4746use std:: collections:: { HashMap , HashSet } ;
4847use std:: time:: Duration ;
48+ use std:: {
49+ cell:: { Cell , RefCell } ,
50+ marker:: PhantomData ,
51+ } ;
4952
5053use js_sys:: { Array , DataView , IteratorNext , Reflect , Uint8Array } ;
5154use wasm_bindgen:: { JsCast , JsValue } ;
@@ -97,10 +100,11 @@ struct OpfsSAHPool {
97100 open_files : RefCell < HashSet < String > > ,
98101 /// A tuple holding the raw pointer to the `sqlite3_vfs` struct and whether it was registered as the default.
99102 vfs : Cell < ( * mut sqlite3_vfs , bool ) > ,
103+ random : fn ( & mut [ u8 ] ) ,
100104}
101105
102106impl OpfsSAHPool {
103- async fn new ( options : & OpfsSAHPoolCfg ) -> Result < OpfsSAHPool > {
107+ async fn new < C : OsCallback > ( options : & OpfsSAHPoolCfg ) -> Result < OpfsSAHPool > {
104108 const OPAQUE_DIR_NAME : & str = ".opaque" ;
105109
106110 let vfs_dir = & options. directory ;
@@ -154,6 +158,7 @@ impl OpfsSAHPool {
154158 is_paused : Cell :: new ( false ) ,
155159 open_files : RefCell :: new ( HashSet :: new ( ) ) ,
156160 vfs : Cell :: new ( ( std:: ptr:: null_mut ( ) , false ) ) ,
161+ random : C :: random,
157162 } ;
158163
159164 pool. acquire_access_handles ( clear_files) . await ?;
@@ -164,7 +169,7 @@ impl OpfsSAHPool {
164169
165170 async fn add_capacity ( & self , n : u32 ) -> Result < u32 > {
166171 for _ in 0 ..n {
167- let opaque = sqlite_wasm_rs :: utils :: random_name ( WasmOsCallback :: random) ;
172+ let opaque = rsqlite_vfs :: random_name ( self . random ) ;
168173 let handle: FileSystemFileHandle =
169174 JsFuture :: from ( self . dh_opaque . get_file_handle_with_options ( & opaque, & {
170175 let options = FileSystemGetFileOptions :: new ( ) ;
@@ -670,9 +675,12 @@ impl SQLiteIoMethods for SyncAccessHandleIoMethods {
670675 }
671676}
672677
673- struct SyncAccessHandleVfs ;
678+ struct SyncAccessHandleVfs < C > ( PhantomData < C > ) ;
674679
675- impl SQLiteVfs < SyncAccessHandleIoMethods > for SyncAccessHandleVfs {
680+ impl < C > SQLiteVfs < SyncAccessHandleIoMethods > for SyncAccessHandleVfs < C >
681+ where
682+ C : OsCallback ,
683+ {
676684 const VERSION : :: std:: os:: raw:: c_int = 2 ;
677685 const MAX_PATH_SIZE : :: std:: os:: raw:: c_int = HEADER_MAX_FILENAME_SIZE as _ ;
678686
@@ -698,15 +706,15 @@ impl SQLiteVfs<SyncAccessHandleIoMethods> for SyncAccessHandleVfs {
698706 }
699707
700708 fn sleep ( dur : Duration ) {
701- WasmOsCallback :: sleep ( dur) ;
709+ C :: sleep ( dur) ;
702710 }
703711
704712 fn random ( buf : & mut [ u8 ] ) {
705- WasmOsCallback :: random ( buf) ;
713+ C :: random ( buf) ;
706714 }
707715
708716 fn epoch_timestamp_in_ms ( ) -> i64 {
709- WasmOsCallback :: epoch_timestamp_in_ms ( )
717+ C :: epoch_timestamp_in_ms ( )
710718 }
711719}
712720
@@ -946,15 +954,18 @@ impl OpfsSAHPoolUtil {
946954///
947955/// If the vfs corresponding to `options.vfs_name` has been registered,
948956/// only return a management tool without register.
949- pub async fn install ( options : & OpfsSAHPoolCfg , default_vfs : bool ) -> Result < OpfsSAHPoolUtil > {
957+ pub async fn install < C : OsCallback > (
958+ options : & OpfsSAHPoolCfg ,
959+ default_vfs : bool ,
960+ ) -> Result < OpfsSAHPoolUtil > {
950961 static REGISTER_GUARD : tokio:: sync:: Mutex < ( ) > = tokio:: sync:: Mutex :: const_new ( ( ) ) ;
951962 let _guard = REGISTER_GUARD . lock ( ) . await ;
952963
953964 let vfs = match registered_vfs ( & options. vfs_name ) ? {
954965 Some ( vfs) => vfs,
955- None => register_vfs :: < SyncAccessHandleIoMethods , SyncAccessHandleVfs > (
966+ None => register_vfs :: < SyncAccessHandleIoMethods , SyncAccessHandleVfs < C > > (
956967 & options. vfs_name ,
957- OpfsSAHPool :: new ( options) . await ?,
968+ OpfsSAHPool :: new :: < C > ( options) . await ?,
958969 default_vfs,
959970 ) ?,
960971 } ;
@@ -971,12 +982,12 @@ mod tests {
971982 OpfsSAHPool , OpfsSAHPoolCfgBuilder , SyncAccessFile , SyncAccessHandleAppData ,
972983 SyncAccessHandleStore ,
973984 } ;
974- use sqlite_wasm_rs :: utils :: { test_suite:: test_vfs_store, VfsAppData } ;
985+ use rsqlite_vfs :: { test_suite:: test_vfs_store, VfsAppData } ;
975986 use wasm_bindgen_test:: wasm_bindgen_test;
976987
977988 #[ wasm_bindgen_test]
978989 async fn test_opfs_vfs_store ( ) {
979- let data = OpfsSAHPool :: new (
990+ let data = OpfsSAHPool :: new :: < sqlite_wasm_rs :: WasmOsCallback > (
980991 & OpfsSAHPoolCfgBuilder :: new ( )
981992 . directory ( "test_opfs_suite" )
982993 . build ( ) ,
0 commit comments