Skip to content

Commit 5608185

Browse files
committed
Split CapnprotoPayload into Encode/Decode
1 parent 96c351a commit 5608185

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

crates/daphne-server/src/storage_proxy_connection/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fmt::Debug;
77

88
use axum::http::StatusCode;
99
use daphne_service_utils::{
10-
capnproto_payload::{CapnprotoPayload, CapnprotoPayloadExt as _},
10+
capnproto_payload::{CapnprotoPayloadEncode, CapnprotoPayloadEncodeExt as _},
1111
durable_requests::{bindings::DurableMethod, DurableRequest, ObjectIdFrom, DO_PATH_PREFIX},
1212
};
1313
use serde::de::DeserializeOwned;
@@ -95,7 +95,7 @@ impl<'d, B: DurableMethod + Debug, P: AsRef<[u8]>> RequestBuilder<'d, B, P> {
9595
}
9696

9797
impl<'d, B: DurableMethod> RequestBuilder<'d, B, [u8; 0]> {
98-
pub fn encode<T: CapnprotoPayload>(self, payload: &T) -> RequestBuilder<'d, B, Vec<u8>> {
98+
pub fn encode<T: CapnprotoPayloadEncode>(self, payload: &T) -> RequestBuilder<'d, B, Vec<u8>> {
9999
self.with_body(payload.encode_to_bytes().unwrap())
100100
}
101101

crates/daphne-service-utils/src/capnproto_payload.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
// Copyright (c) 2024 Cloudflare, Inc. All rights reserved.
22
// SPDX-License-Identifier: BSD-3-Clause
33

4-
pub trait CapnprotoPayload {
4+
pub trait CapnprotoPayloadEncode {
5+
fn encode_to_builder(&self) -> capnp::message::Builder<capnp::message::HeapAllocator>;
6+
}
7+
8+
pub trait CapnprotoPayloadEncodeExt {
9+
fn encode_to_bytes(&self) -> capnp::Result<Vec<u8>>;
10+
}
11+
12+
pub trait CapnprotoPayloadDecode {
513
fn decode_from_reader(
614
reader: capnp::message::Reader<capnp::serialize::OwnedSegments>,
715
) -> capnp::Result<Self>
816
where
917
Self: Sized;
10-
11-
fn encode_to_builder(&self) -> capnp::message::Builder<capnp::message::HeapAllocator>;
1218
}
1319

14-
pub trait CapnprotoPayloadExt {
20+
pub trait CapnprotoPayloadDecodeExt {
1521
fn decode_from_bytes(bytes: &[u8]) -> capnp::Result<Self>
1622
where
1723
Self: Sized;
18-
fn encode_to_bytes(&self) -> capnp::Result<Vec<u8>>;
1924
}
2025

21-
impl<T> CapnprotoPayloadExt for T
26+
impl<T> CapnprotoPayloadEncodeExt for T
2227
where
23-
T: CapnprotoPayload,
28+
T: CapnprotoPayloadEncode,
2429
{
2530
fn encode_to_bytes(&self) -> capnp::Result<Vec<u8>> {
2631
let mut buf = Vec::new();
2732
let message = self.encode_to_builder();
2833
capnp::serialize_packed::write_message(&mut buf, &message)?;
2934
Ok(buf)
3035
}
36+
}
3137

38+
impl<T> CapnprotoPayloadDecodeExt for T
39+
where
40+
T: CapnprotoPayloadDecode,
41+
{
3242
fn decode_from_bytes(bytes: &[u8]) -> capnp::Result<Self>
3343
where
3444
Self: Sized,

crates/daphne-service-utils/src/durable_requests/bindings/aggregate_store.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use daphne::{
1111
use serde::{Deserialize, Serialize};
1212

1313
use crate::{
14-
capnproto_payload::CapnprotoPayload,
14+
capnproto_payload::{CapnprotoPayloadDecode, CapnprotoPayloadEncode},
1515
durable_request_capnp::{aggregate_store_merge_req, dap_aggregate_share},
1616
durable_requests::ObjectIdFrom,
1717
};
@@ -68,7 +68,7 @@ pub struct AggregateStoreMergeOptions {
6868
pub skip_replay_protection: bool,
6969
}
7070

71-
impl CapnprotoPayload for AggregateStoreMergeReq {
71+
impl CapnprotoPayloadEncode for AggregateStoreMergeReq {
7272
fn encode_to_builder(&self) -> capnp::message::Builder<capnp::message::HeapAllocator> {
7373
let Self {
7474
contained_reports,
@@ -162,7 +162,9 @@ impl CapnprotoPayload for AggregateStoreMergeReq {
162162
}
163163
message
164164
}
165+
}
165166

167+
impl CapnprotoPayloadDecode for AggregateStoreMergeReq {
166168
fn decode_from_reader(
167169
reader: capnp::message::Reader<capnp::serialize::OwnedSegments>,
168170
) -> capnp::Result<Self> {
@@ -285,7 +287,9 @@ mod test {
285287
};
286288
use rand::{thread_rng, Rng};
287289

288-
use crate::capnproto_payload::CapnprotoPayloadExt as _;
290+
use crate::capnproto_payload::{
291+
CapnprotoPayloadDecodeExt as _, CapnprotoPayloadEncodeExt as _,
292+
};
289293

290294
use super::*;
291295

crates/daphne-worker/src/durable/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(crate) mod test_state_cleaner;
2525

2626
use crate::tracing_utils::shorten_paths;
2727
use daphne_service_utils::{
28-
capnproto_payload::{CapnprotoPayload, CapnprotoPayloadExt},
28+
capnproto_payload::{CapnprotoPayloadDecode, CapnprotoPayloadDecodeExt},
2929
durable_requests::bindings::DurableMethod,
3030
};
3131
use serde::{Deserialize, Serialize};
@@ -210,7 +210,7 @@ pub(crate) async fn state_set_if_not_exists<T: for<'a> Deserialize<'a> + Seriali
210210

211211
async fn req_parse<T>(req: &mut Request) -> Result<T>
212212
where
213-
T: CapnprotoPayload,
213+
T: CapnprotoPayloadDecode,
214214
{
215215
T::decode_from_bytes(&req.bytes().await?).map_err(|e| Error::RustError(e.to_string()))
216216
}

0 commit comments

Comments
 (0)