Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass slice instead of Vec during map serialization. #284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 16 additions & 16 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ extern "C" {
}

pub fn set_map(map_type: MapType, map: Vec<(&str, &str)>) -> Result<(), Status> {
let serialized_map = utils::serialize_map(map);
let serialized_map = utils::serialize_map(&map);
unsafe {
match proxy_set_header_map_pairs(map_type, serialized_map.as_ptr(), serialized_map.len()) {
Status::Ok => Ok(()),
Expand All @@ -200,7 +200,7 @@ pub fn set_map(map_type: MapType, map: Vec<(&str, &str)>) -> Result<(), Status>
}

pub fn set_map_bytes(map_type: MapType, map: Vec<(&str, &[u8])>) -> Result<(), Status> {
let serialized_map = utils::serialize_map_bytes(map);
let serialized_map = utils::serialize_map_bytes(&map);
unsafe {
match proxy_set_header_map_pairs(map_type, serialized_map.as_ptr(), serialized_map.len()) {
Status::Ok => Ok(()),
Expand Down Expand Up @@ -710,7 +710,7 @@ pub fn send_http_response(
headers: Vec<(&str, &str)>,
body: Option<&[u8]>,
) -> Result<(), Status> {
let serialized_headers = utils::serialize_map(headers);
let serialized_headers = utils::serialize_map(&headers);
unsafe {
match proxy_send_local_response(
status_code,
Expand All @@ -733,7 +733,7 @@ pub fn send_grpc_response(
grpc_status_message: Option<&str>,
custom_metadata: Vec<(&str, &[u8])>,
) -> Result<(), Status> {
let serialized_custom_metadata = utils::serialize_map_bytes(custom_metadata);
let serialized_custom_metadata = utils::serialize_map_bytes(&custom_metadata);
unsafe {
match proxy_send_local_response(
200,
Expand Down Expand Up @@ -773,8 +773,8 @@ pub fn dispatch_http_call(
trailers: Vec<(&str, &str)>,
timeout: Duration,
) -> Result<u32, Status> {
let serialized_headers = utils::serialize_map(headers);
let serialized_trailers = utils::serialize_map(trailers);
let serialized_headers = utils::serialize_map(&headers);
let serialized_trailers = utils::serialize_map(&trailers);
let mut return_token: u32 = 0;
unsafe {
match proxy_http_call(
Expand Down Expand Up @@ -826,7 +826,7 @@ pub fn dispatch_grpc_call(
timeout: Duration,
) -> Result<u32, Status> {
let mut return_callout_id = 0;
let serialized_initial_metadata = utils::serialize_map_bytes(initial_metadata);
let serialized_initial_metadata = utils::serialize_map_bytes(&initial_metadata);
unsafe {
match proxy_grpc_call(
upstream_name.as_ptr(),
Expand Down Expand Up @@ -874,7 +874,7 @@ pub fn open_grpc_stream(
initial_metadata: Vec<(&str, &[u8])>,
) -> Result<u32, Status> {
let mut return_stream_id = 0;
let serialized_initial_metadata = utils::serialize_map_bytes(initial_metadata);
let serialized_initial_metadata = utils::serialize_map_bytes(&initial_metadata);
unsafe {
match proxy_grpc_stream(
upstream_name.as_ptr(),
Expand Down Expand Up @@ -1159,18 +1159,18 @@ mod utils {
bytes
}

pub(super) fn serialize_map(map: Vec<(&str, &str)>) -> Bytes {
pub(super) fn serialize_map(map: &[(&str, &str)]) -> Bytes {
let mut size: usize = 4;
for (name, value) in &map {
for (name, value) in map {
size += name.len() + value.len() + 10;
}
let mut bytes: Bytes = Vec::with_capacity(size);
bytes.extend_from_slice(&map.len().to_le_bytes());
for (name, value) in &map {
for (name, value) in map {
bytes.extend_from_slice(&name.len().to_le_bytes());
bytes.extend_from_slice(&value.len().to_le_bytes());
}
for (name, value) in &map {
for (name, value) in map {
bytes.extend_from_slice(name.as_bytes());
bytes.push(0);
bytes.extend_from_slice(value.as_bytes());
Expand All @@ -1179,18 +1179,18 @@ mod utils {
bytes
}

pub(super) fn serialize_map_bytes(map: Vec<(&str, &[u8])>) -> Bytes {
pub(super) fn serialize_map_bytes(map: &[(&str, &[u8])]) -> Bytes {
let mut size: usize = 4;
for (name, value) in &map {
for (name, value) in map {
size += name.len() + value.len() + 10;
}
let mut bytes: Bytes = Vec::with_capacity(size);
bytes.extend_from_slice(&map.len().to_le_bytes());
for (name, value) in &map {
for (name, value) in map {
bytes.extend_from_slice(&name.len().to_le_bytes());
bytes.extend_from_slice(&value.len().to_le_bytes());
}
for (name, value) in &map {
for (name, value) in map {
bytes.extend_from_slice(name.as_bytes());
bytes.push(0);
bytes.extend_from_slice(value);
Expand Down
Loading