Skip to content

Commit

Permalink
Revert "use &[u8] instead of Bytes for requests"
Browse files Browse the repository at this point in the history
This reverts commit e179db1.
  • Loading branch information
gbj committed Jan 19, 2024
1 parent 25120c0 commit a519859
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
7 changes: 6 additions & 1 deletion server_fn/src/codec/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ where
let mut buffer: Vec<u8> = Vec::new();
ciborium::ser::into_writer(&self, &mut buffer)
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
Request::try_new_post_bytes(path, accepts, Cbor::CONTENT_TYPE, &buffer)
Request::try_new_post_bytes(
path,
accepts,
Cbor::CONTENT_TYPE,
Bytes::from(buffer),
)
}
}

Expand Down
10 changes: 3 additions & 7 deletions server_fn/src/codec/rkyv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ where
) -> Result<Request, ServerFnError<CustErr>> {
let encoded = rkyv::to_bytes::<T, 1024>(&self)
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
Request::try_new_post_bytes(
path,
accepts,
Rkyv::CONTENT_TYPE,
encoded.as_ref(),
)
let bytes = Bytes::copy_from_slice(encoded.as_ref());
Request::try_new_post_bytes(path, accepts, Rkyv::CONTENT_TYPE, bytes)
}
}

Expand All @@ -54,7 +50,7 @@ where
{
async fn from_req(req: Request) -> Result<Self, ServerFnError<CustErr>> {
let body_bytes = req.try_into_bytes().await?;
rkyv::from_bytes::<T>(&body_bytes)
rkyv::from_bytes::<T>(body_bytes.as_ref())
.map_err(|e| ServerFnError::Args(e.to_string()))
}
}
Expand Down
3 changes: 2 additions & 1 deletion server_fn/src/request/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ impl<CustErr> ClientReq<CustErr> for BrowserRequest {
path: &str,
accepts: &str,
content_type: &str,
body: &[u8],
body: Bytes,
) -> Result<Self, ServerFnError<CustErr>> {
let server_url = get_server_url();
let mut url = String::with_capacity(server_url.len() + path.len());
url.push_str(server_url);
url.push_str(path);
let body: &[u8] = &body;
let body = Uint8Array::from(body).buffer();
Ok(Self(SendWrapper::new(
Request::post(&url)
Expand Down
2 changes: 1 addition & 1 deletion server_fn/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
path: &str,
content_type: &str,
accepts: &str,
body: &[u8],
body: Bytes,
) -> Result<Self, ServerFnError<CustErr>>;

/// Attempts to construct a new `POST` request with form data as the body.
Expand Down
4 changes: 2 additions & 2 deletions server_fn/src/request/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ impl<CustErr> ClientReq<CustErr> for Request {
path: &str,
accepts: &str,
content_type: &str,
body: &[u8],
body: Bytes,
) -> Result<Self, ServerFnError<CustErr>> {
let url = format!("{}{}", get_server_url(), path);
CLIENT
.post(url)
.header(CONTENT_TYPE, content_type)
.header(ACCEPT, accepts)
.body(body.to_owned())
.body(body)
.build()
.map_err(|e| ServerFnError::Request(e.to_string()))
}
Expand Down

0 comments on commit a519859

Please sign in to comment.