Skip to content

Commit 406899d

Browse files
committed
for_each instead of foreach
1 parent ed9147f commit 406899d

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

examples/scan_keys.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// 1. `scan_keys` - scans all keys in the database and returns their names as an array of RedisString.
66
// 2. `scan_key <key>` - scans all fields by using a closure and a while loop, thus allowing an early stop. Don't use the early stop but collects all the field/value pairs as an array of RedisString.
7-
// 3. `scan_key_foreach <key>` - scans all fields and values in a hash key using a closure that stores the field/value pairs as an array of RedisString.
7+
// 3. `scan_key_for_each <key>` - scans all fields and values in a hash key using a closure that stores the field/value pairs as an array of RedisString.
88

99
use redis_module::{
1010
key::{KeyFlags, RedisKey}, redis_module, Context, KeysCursor, RedisError, RedisResult, RedisString, RedisValue, ScanKeyCursor
@@ -48,7 +48,7 @@ fn scan_key(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
4848

4949
/// Scans all fields and values in a hash key and returns them as an array of RedisString.
5050
/// The command takes one argument: the name of the hash key to scan.
51-
fn scan_key_foreach(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
51+
fn scan_key_for_each(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
5252
// only argument is the key name
5353
if args.len() != 2 {
5454
return Err(RedisError::WrongArity);
@@ -59,7 +59,7 @@ fn scan_key_foreach(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
5959
let cursor = ScanKeyCursor::new(key);
6060

6161
let mut res = Vec::new();
62-
cursor.foreach(|_key, field, value| {
62+
cursor.for_each(|_key, field, value| {
6363
res.push(RedisValue::BulkRedisString(field.clone()));
6464
res.push(RedisValue::BulkRedisString(value.clone()));
6565
});
@@ -78,6 +78,6 @@ redis_module! {
7878
commands: [
7979
["scan_keys", scan_keys, "readonly", 0, 0, 0, ""],
8080
["scan_key", scan_key, "readonly", 0, 0, 0, ""],
81-
["scan_key_foreach", scan_key_foreach, "readonly", 0, 0, 0, ""],
81+
["scan_key_for_each", scan_key_for_each, "readonly", 0, 0, 0, ""],
8282
],
8383
}

src/context/key_cursor.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{key::RedisKey, raw, RedisString};
77

88
/// A cursor to scan field/value pairs of a (hash) key.
99
///
10-
/// It provides access via a closure given to [`ScanKeyCursor::foreach`] or if you need more control, you can use [`ScanKeyCursor::scan`]
10+
/// It provides access via a closure given to [`ScanKeyCursor::for_each`] or if you need more control, you can use [`ScanKeyCursor::scan`]
1111
/// and implement your own loop, e.g. to allow an early stop.
1212
///
1313
/// ## Example usage
@@ -21,12 +21,12 @@ use crate::{key::RedisKey, raw, RedisString};
2121
/// The following example command implementation scans all fields and values in the hash key and returns them as an array of RedisString.
2222
///
2323
/// ```ignore
24-
/// fn example_scan_key_foreach(ctx: &Context) -> RedisResult {
24+
/// fn example_scan_key_for_each(ctx: &Context) -> RedisResult {
2525
/// let key = ctx.open_key_with_flags("user:123", KeyFlags::NOEFFECTS | KeyFlags::NOEXPIRE | KeyFlags::ACCESS_EXPIRED );
2626
/// let cursor = ScanKeyCursor::new(key);
2727
///
2828
/// let res = RefCell::new(Vec::new());
29-
/// cursor.foreach(|_key, field, value| {
29+
/// cursor.for_each(|_key, field, value| {
3030
/// let mut res = res.borrow_mut();
3131
/// res.push(RedisValue::BulkRedisString(field.clone()));
3232
/// res.push(RedisValue::BulkRedisString(value.clone()));
@@ -64,8 +64,7 @@ impl ScanKeyCursor {
6464
}
6565

6666
pub fn scan<F: FnMut(&RedisKey, &RedisString, &RedisString)>(&self, f: F) -> bool {
67-
// the following is the callback definition
68-
// foreach `ScanKey` call the callback may be called multiple times
67+
// the following is the callback definition. The callback may be called multiple times per `RedisModule_ScanKey` invocation.
6968
use pimpl::scan_callback;
7069

7170
// Safety: The c-side initialized the function ptr and it is is never changed,
@@ -82,10 +81,8 @@ impl ScanKeyCursor {
8281
res != 0
8382
}
8483

85-
/// Implements a callback based foreach loop over all fields and values in the hash key, use that for optimal performance.
86-
pub fn foreach<F: FnMut(&RedisKey, &RedisString, &RedisString)>(&self, mut f: F) {
87-
// the following is the callback definition
88-
// foreach `ScanKey` call the callback may be called multiple times
84+
/// Implements a callback based for_each loop over all fields and values in the hash key, use that for optimal performance.
85+
pub fn for_each<F: FnMut(&RedisKey, &RedisString, &RedisString)>(&self, mut f: F) {
8986
while self.scan(&mut f) {
9087
// do nothing, the callback does the work
9188
}
@@ -102,9 +99,9 @@ impl Drop for ScanKeyCursor {
10299
mod pimpl {
103100
use super::*;
104101

105-
/// The callback that is used by [`ScanKeyCursor::scan`] and [`ScanKeyCursor::foreach`] as argument to `RedisModule_ScanKey`.
102+
/// The callback that is used by [`ScanKeyCursor::scan`] and [`ScanKeyCursor::for_each`] as argument to `RedisModule_ScanKey`.
106103
///
107-
/// The `data` pointer is the closure given to [`ScanKeyCursor::foreach`] and the callback forwards
104+
/// The `data` pointer is the closure given to [`ScanKeyCursor::for_each`] and the callback forwards
108105
/// references to the key, field and value to that closure.
109106
pub(super) unsafe extern "C" fn scan_callback<
110107
F: FnMut(&RedisKey, &RedisString, &RedisString),

tests/integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn test_scan_key_for_each() -> Result<()> {
227227
.query::<()>(&mut con)
228228
.with_context(|| "failed to hset")?;
229229

230-
let res: Vec<String> = redis::cmd("scan_key_foreach")
230+
let res: Vec<String> = redis::cmd("scan_key_for_each")
231231
.arg(&["user:123"])
232232
.query(&mut con)?;
233233
assert_eq!(&res, &["name", "Alice", "age", "29", "location", "Austin"]);

0 commit comments

Comments
 (0)