Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:

steps:
- uses: dtolnay/rust-toolchain@1.87.0
- name: Enable symlinks on Windows
if: runner.os == 'Windows'
run: git config --global core.symlinks true
- uses: actions/checkout@v4
with:
submodules: true
Expand Down Expand Up @@ -165,6 +168,9 @@ jobs:

steps:
- uses: dtolnay/rust-toolchain@1.87.0
- name: Enable symlinks on Windows
if: runner.os == 'Windows'
run: git config --global core.symlinks true
- uses: actions/checkout@v4
with:
submodules: true
Expand Down
22 changes: 0 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions test/src/sql_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl DurableObject for SqlCounter {
fn new(state: State, _env: Env) -> Self {
let sql = state.storage().sql();
// Create table if it does not exist. Note: `exec` is synchronous.
sql.exec("CREATE TABLE IF NOT EXISTS counter(value INTEGER);", None)
sql.exec("CREATE TABLE IF NOT EXISTS counter(value INTEGER);", [])
.expect("create table");
Self { sql }
}
Expand Down Expand Up @@ -43,15 +43,15 @@ impl SqlCounter {

let rows: Vec<Row> = self
.sql
.exec("SELECT value FROM counter LIMIT 1;", None)?
.exec("SELECT value FROM counter LIMIT 1;", [])?
.to_array()?;
let current = rows.first().map_or(0, |r| r.value);
let next = current + 1;

// Upsert new value – simplest way: delete and insert again.
self.sql.exec("DELETE FROM counter;", None)?;
self.sql.exec("DELETE FROM counter;", [])?;
self.sql
.exec("INSERT INTO counter(value) VALUES (?);", vec![next.into()])?;
.exec("INSERT INTO counter(value) VALUES (?);", [next.into()])?;

Response::ok(format!("SQL counter is now {next}"))
}
Expand All @@ -77,9 +77,9 @@ impl SqlCounter {
};

// Store the safe value
self.sql.exec("DELETE FROM counter;", None)?;
self.sql.exec("DELETE FROM counter;", [])?;
self.sql
.exec("INSERT INTO counter(value) VALUES (?);", vec![safe_value])?;
.exec("INSERT INTO counter(value) VALUES (?);", [safe_value])?;

Response::ok(format!("Successfully stored large value: {large_value}"))
}
Expand Down
38 changes: 16 additions & 22 deletions test/src/sql_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ impl DurableObject for SqlIterator {
// Create table and seed with test data
sql.exec(
"CREATE TABLE IF NOT EXISTS products(id INTEGER PRIMARY KEY, name TEXT, price REAL, in_stock INTEGER);",
None,
[],
).expect("create table");

sql.exec(
"CREATE TABLE IF NOT EXISTS blob_data(id INTEGER PRIMARY KEY, name TEXT, data BLOB);",
None,
[],
)
.expect("create blob table");

// Check if we need to seed data
let count: Vec<serde_json::Value> = sql
.exec("SELECT COUNT(*) as count FROM products;", None)
.exec("SELECT COUNT(*) as count FROM products;", [])
.expect("count query")
.to_array()
.expect("count result");
Expand All @@ -103,14 +103,14 @@ impl DurableObject for SqlIterator {
for (name, price, in_stock) in products {
sql.exec(
"INSERT INTO products(name, price, in_stock) VALUES (?, ?, ?);",
vec![name.into(), price.into(), i32::from(in_stock).into()],
[name.into(), price.into(), i32::from(in_stock).into()],
)
.expect("insert product");
}
}

let blob_count: Vec<serde_json::Value> = sql
.exec("SELECT COUNT(*) as count FROM blob_data;", None)
.exec("SELECT COUNT(*) as count FROM blob_data;", [])
.expect("blob count query")
.to_array()
.expect("blob count result");
Expand All @@ -135,7 +135,7 @@ impl DurableObject for SqlIterator {
for (name, data) in blob_test_data {
sql.exec(
"INSERT INTO blob_data(name, data) VALUES (?, ?);",
vec![name.into(), SqlStorageValue::Blob(data)],
[name.into(), SqlStorageValue::Blob(data)],
)
.expect("insert blob data");
}
Expand All @@ -162,7 +162,7 @@ impl DurableObject for SqlIterator {

impl SqlIterator {
fn handle_next(&self) -> Result<Response> {
let cursor = self.sql.exec("SELECT * FROM products ORDER BY id;", None)?;
let cursor = self.sql.exec("SELECT * FROM products ORDER BY id;", [])?;

let mut results = Vec::new();
let iterator = cursor.next::<Product>();
Expand Down Expand Up @@ -190,7 +190,7 @@ impl SqlIterator {
}

fn handle_raw(&self) -> Result<Response> {
let cursor = self.sql.exec("SELECT * FROM products ORDER BY id;", None)?;
let cursor = self.sql.exec("SELECT * FROM products ORDER BY id;", [])?;

let mut results = Vec::new();
let column_names = cursor.column_names();
Expand All @@ -216,7 +216,7 @@ impl SqlIterator {
}

fn handle_next_invalid(&self) -> Result<Response> {
let cursor = self.sql.exec("SELECT * FROM products ORDER BY id;", None)?;
let cursor = self.sql.exec("SELECT * FROM products ORDER BY id;", [])?;

let mut results = Vec::new();
let iterator = cursor.next::<BadProduct>();
Expand Down Expand Up @@ -244,9 +244,7 @@ impl SqlIterator {
}

fn handle_blob_next(&self) -> Result<Response> {
let cursor = self
.sql
.exec("SELECT * FROM blob_data ORDER BY id;", None)?;
let cursor = self.sql.exec("SELECT * FROM blob_data ORDER BY id;", [])?;

let mut results = Vec::new();
let iterator = cursor.raw();
Expand Down Expand Up @@ -285,9 +283,7 @@ impl SqlIterator {
}

fn handle_blob_raw(&self) -> Result<Response> {
let cursor = self
.sql
.exec("SELECT * FROM blob_data ORDER BY id;", None)?;
let cursor = self.sql.exec("SELECT * FROM blob_data ORDER BY id;", [])?;

let mut results = Vec::new();
let column_names = cursor.column_names();
Expand Down Expand Up @@ -341,18 +337,18 @@ impl SqlIterator {
// Insert test data
self.sql.exec(
"INSERT INTO blob_data(name, data) VALUES (?, ?);",
vec![test_name.into(), SqlStorageValue::Blob(test_data.clone())],
[test_name.into(), SqlStorageValue::Blob(test_data.clone())],
)?;

// Read it back using both methods (raw iterator approach for both)
let cursor_next = self.sql.exec(
"SELECT * FROM blob_data WHERE name = ? ORDER BY id DESC LIMIT 1;",
vec![test_name.into()],
[test_name.into()],
)?;

let cursor_raw = self.sql.exec(
"SELECT * FROM blob_data WHERE name = ? ORDER BY id DESC LIMIT 1;",
vec![test_name.into()],
[test_name.into()],
)?;

let mut results = Vec::new();
Expand Down Expand Up @@ -400,10 +396,8 @@ impl SqlIterator {
}

// Clean up test data
self.sql.exec(
"DELETE FROM blob_data WHERE name = ?;",
vec![test_name.into()],
)?;
self.sql
.exec("DELETE FROM blob_data WHERE name = ?;", [test_name.into()])?;

let response_body = format!("blob-roundtrip test results:\n{}", results.join("\n"));

Expand Down
5 changes: 4 additions & 1 deletion worker/src/d1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ impl D1Database {
/// Batch execute one or more statements against the database.
///
/// Returns the results in the same order as the provided statements.
pub async fn batch(&self, statements: Vec<D1PreparedStatement>) -> Result<Vec<D1Result>> {
pub async fn batch(
&self,
statements: impl IntoIterator<Item = D1PreparedStatement>,
) -> Result<Vec<D1Result>> {
let statements = statements.into_iter().map(|s| s.0).collect::<Array>();
let results = JsFuture::from(self.0.batch(statements)?).await;
let results = cast_to_d1_error(results)?;
Expand Down
20 changes: 16 additions & 4 deletions worker/src/durable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ impl Storage {
}

/// Retrieves the values associated with each of the provided keys.
pub async fn get_multiple(&self, keys: Vec<impl Deref<Target = str>>) -> Result<Map> {
pub async fn get_multiple<S: Deref<Target = str>, I: IntoIterator<Item = S>>(
&self,
keys: I,
) -> Result<Map> {
let keys = self.inner.get_multiple(
keys.into_iter()
.map(|key| JsValue::from(key.deref()))
Expand Down Expand Up @@ -426,7 +429,10 @@ impl Storage {

/// Deletes the provided keys and their associated values. Returns a count of the number of
/// key-value pairs deleted.
pub async fn delete_multiple(&self, keys: Vec<impl Deref<Target = str>>) -> Result<usize> {
pub async fn delete_multiple(
&self,
keys: impl IntoIterator<Item = impl Deref<Target = str>>,
) -> Result<usize> {
let fut: JsFuture = self
.inner
.delete_multiple(
Expand Down Expand Up @@ -590,7 +596,10 @@ impl Transaction {
.map_err(Error::from)
}

pub async fn get_multiple(&self, keys: Vec<impl Deref<Target = str>>) -> Result<Map> {
pub async fn get_multiple<I: IntoIterator<Item = D>, D: Deref<Target = str>>(
&self,
keys: I,
) -> Result<Map> {
let keys = self.inner.get_multiple(
keys.into_iter()
.map(|key| JsValue::from(key.deref()))
Expand Down Expand Up @@ -629,7 +638,10 @@ impl Transaction {
.map_err(Error::from)
}

pub async fn delete_multiple(&self, keys: Vec<impl Deref<Target = str>>) -> Result<usize> {
pub async fn delete_multiple<I: IntoIterator<Item = D>, D: Deref<Target = str>>(
&self,
keys: I,
) -> Result<usize> {
let fut: JsFuture = self
.inner
.delete_multiple(
Expand Down
4 changes: 2 additions & 2 deletions worker/src/r2/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ impl ListOptionsBuilder<'_> {
/// .await?;
/// }
/// ```
pub fn include(mut self, include: Vec<Include>) -> Self {
self.include = Some(include);
pub fn include(mut self, include: impl IntoIterator<Item = Include>) -> Self {
self.include = Some(include.into_iter().collect());
self
}

Expand Down
5 changes: 4 additions & 1 deletion worker/src/r2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ impl Bucket {
/// pairs globally.
///
/// Up to 1000 keys may be deleted per call.
pub async fn delete_multiple(&self, keys: Vec<impl Deref<Target = str>>) -> Result<()> {
pub async fn delete_multiple(
&self,
keys: impl IntoIterator<Item = impl Deref<Target = str>>,
) -> Result<()> {
let fut: JsFuture = self
.inner
.delete_multiple(keys.into_iter().map(|key| JsValue::from(&*key)).collect())?
Expand Down
7 changes: 6 additions & 1 deletion worker/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,12 @@ impl<'a, D: 'a> Router<'a, D> {
self
}

fn add_handler(&mut self, pattern: &str, func: Handler<'a, D>, methods: Vec<Method>) {
fn add_handler(
&mut self,
pattern: &str,
func: Handler<'a, D>,
methods: impl IntoIterator<Item = Method>,
) {
for method in methods {
self.handlers
.entry(method.clone())
Expand Down
Loading
Loading