Skip to content

Commit a16090a

Browse files
committed
refactor: make parameter types for WAE less strict
1 parent 219ac34 commit a16090a

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

worker-sandbox/src/analytics_engine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub async fn handle_analytics_event(
1616
let request_id = Uuid::new_v4();
1717
// Build the event and write it to analytics engine
1818
let point = AnalyticsEngineDataPointBuilder::new()
19-
.indexes(vec!["index1"].as_slice())
19+
.indexes(vec!["index1"])
2020
.add_blob(req.method().as_ref()) // blob1
2121
.add_blob(request_id.as_bytes().as_ref()) // blob2
2222
.add_double(200)
@@ -25,7 +25,7 @@ pub async fn handle_analytics_event(
2525

2626
// Or write it directly from the builder using write_to
2727
AnalyticsEngineDataPointBuilder::new()
28-
.indexes(vec!["index1"].as_slice())
28+
.indexes(["index1"])
2929
.add_blob(req.method().as_ref()) // blob1
3030
.add_blob(req.method().as_ref()) // blob2
3131
.add_double(200)

worker/src/analytics_engine.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ impl From<Vec<u8>> for BlobType {
9999
}
100100
}
101101

102+
impl<const COUNT: usize> From<&[u8; COUNT]> for BlobType {
103+
fn from(value: &[u8; COUNT]) -> Self {
104+
BlobType::Blob(value.to_vec())
105+
}
106+
}
107+
102108
#[derive(Clone)]
103109
pub struct AnalyticsEngineDataPoint {
104110
indexes: Array,
@@ -137,12 +143,12 @@ impl AnalyticsEngineDataPointBuilder {
137143
/// use worker::AnalyticsEngineDataPointBuilder;
138144
///
139145
/// let data = AnalyticsEngineDataPointBuilder::new()
140-
/// .indexes(vec!["index1"].as_slice())
146+
/// .indexes(["index1"])
141147
/// .build();
142148
/// ```
143-
pub fn indexes(mut self, indexes: &[&str]) -> Self {
149+
pub fn indexes<'index>(mut self, indexes: impl AsRef<[&'index str]>) -> Self {
144150
let values = Array::new();
145-
for idx in indexes {
151+
for idx in indexes.as_ref() {
146152
values.push(&JsValue::from_str(idx));
147153
}
148154
self.indexes = values;
@@ -162,7 +168,7 @@ impl AnalyticsEngineDataPointBuilder {
162168
/// ```
163169
/// use worker::AnalyticsEngineDataPointBuilder;
164170
/// let point = AnalyticsEngineDataPointBuilder::new()
165-
/// .indexes(vec!["index1"].into())
171+
/// .indexes(["index1"])
166172
/// .add_double(25) // double1
167173
/// .add_double(0.5) // double2
168174
/// .build();
@@ -187,16 +193,16 @@ impl AnalyticsEngineDataPointBuilder {
187193
/// ```
188194
/// use worker::AnalyticsEngineDataPointBuilder;
189195
/// let point = AnalyticsEngineDataPointBuilder::new()
190-
/// .indexes(vec!["index1"].into())
196+
/// .indexes(["index1"])
191197
/// .add_double(1) // value will be replaced by the following line
192-
/// .doubles(vec![1, 2, 3].into()) // sets double1, double2 and double3
198+
/// .doubles([1, 2, 3]) // sets double1, double2 and double3
193199
/// .build();
194200
/// println!("{:?}", point);
195201
/// ```
196-
pub fn doubles(mut self, doubles: &[f64]) -> Self {
202+
pub fn doubles(mut self, doubles: impl IntoIterator<Item = f64>) -> Self {
197203
let values = Array::new();
198204
for n in doubles {
199-
values.push(&JsValue::from_f64(*n));
205+
values.push(&JsValue::from_f64(n));
200206
}
201207
self.doubles = values;
202208
self
@@ -215,7 +221,7 @@ impl AnalyticsEngineDataPointBuilder {
215221
/// ```
216222
/// use worker::AnalyticsEngineDataPointBuilder;
217223
/// let point = AnalyticsEngineDataPointBuilder::new()
218-
/// .indexes(vec!["index1"].into())
224+
/// .indexes(["index1"])
219225
/// .add_blob("Seattle") // blob1
220226
/// .add_blob("USA") // blob2
221227
/// .add_blob("pro_sensor_9000") // blob3
@@ -241,12 +247,12 @@ impl AnalyticsEngineDataPointBuilder {
241247
/// ```
242248
/// use worker::AnalyticsEngineDataPointBuilder;
243249
/// let point = AnalyticsEngineDataPointBuilder::new()
244-
/// .indexes(vec!["index1"].into())
245-
/// .blobs(vec!["Seattle", "USA", "pro_sensor_9000"]) // sets blob1, blob2, and blob3
250+
/// .indexes(["index1"])
251+
/// .blobs(["Seattle", "USA", "pro_sensor_9000"]) // sets blob1, blob2, and blob3
246252
/// .build();
247253
/// println!("{:?}", point);
248254
/// ```
249-
pub fn blobs(mut self, blobs: Vec<impl Into<BlobType>>) -> Self {
255+
pub fn blobs(mut self, blobs: impl IntoIterator<Item = impl Into<BlobType>>) -> Self {
250256
let values = Array::new();
251257
for blob in blobs {
252258
let value = blob.into();

0 commit comments

Comments
 (0)