@@ -63,12 +63,21 @@ impl ScanKeyCursor {
6363 unsafe { raw:: RedisModule_ScanCursorRestart . unwrap ( ) ( self . inner_cursor ) } ;
6464 }
6565
66+ /// Implements a call to `RedisModule_ScanKey` and calls the given closure for each callback invocation by ScanKey.
67+ /// Returns `true` if there are more fields to scan, `false` otherwise.
68+ ///
69+ /// The callback may be called multiple times per `RedisModule_ScanKey` invocation.
70+ ///
71+ /// ## Example
72+ ///
73+ /// ```
74+ /// while cursor.scan(|_key, field, value| {
75+ /// // do something with field and value
76+ /// }) {
77+ /// // do something between scans if needed, like an early stop
78+ /// }
6679 pub fn scan < F : FnMut ( & RedisKey , & RedisString , & RedisString ) > ( & self , f : F ) -> bool {
67- // The following is the callback definition. The callback may be called multiple times per `RedisModule_ScanKey` invocation.
68- // The callback is used by [`ScanKeyCursor::scan`] and [`ScanKeyCursor::for_each`] as argument to `RedisModule_ScanKey`.
69- //
70- // The `data` pointer is the closure given to [`ScanKeyCursor::scan`] or [`ScanKeyCursor::for_each`].
71- // The callback forwards references to the key, field and value to that closure.
80+
7281 unsafe extern "C" fn scan_callback <
7382 F : FnMut ( & RedisKey , & RedisString , & RedisString ) ,
7483 > (
@@ -95,8 +104,10 @@ impl ScanKeyCursor {
95104
96105 // Safety: The c-side initialized the function ptr and it is is never changed,
97106 // i.e. after module initialization the function pointers stay valid till the end of the program.
107+ let scan_key = unsafe { raw:: RedisModule_ScanKey . unwrap ( ) } ;
108+
98109 let res = unsafe {
99- raw :: RedisModule_ScanKey . unwrap ( ) (
110+ scan_key (
100111 self . key . key_inner ,
101112 self . inner_cursor ,
102113 Some ( scan_callback :: < F > ) ,
@@ -107,7 +118,8 @@ impl ScanKeyCursor {
107118 res != 0
108119 }
109120
110- /// Implements a callback based for_each loop over all fields and values in the hash key, use that for optimal performance.
121+ /// Implements a callback based for_each loop over all fields and values in the hash key.
122+ /// If you need more control, e.g. stopping after a scan invocation, then use [`ScanKeyCursor::scan`] directly.
111123 pub fn for_each < F : FnMut ( & RedisKey , & RedisString , & RedisString ) > ( & self , mut f : F ) {
112124 while self . scan ( & mut f) {
113125 // do nothing, the callback does the work
0 commit comments