Skip to content

Commit

Permalink
Stop using "our" Bytes type.
Browse files Browse the repository at this point in the history
It's a simple alias to Vec<u8>, so it doesn't provide any value,
and it conflicts with a popular Bytes type from the bytes crate.

This is a non-breaking change, since we retain the public alias.

Signed-off-by: Piotr Sikora <[email protected]>
  • Loading branch information
PiotrSikora committed Oct 28, 2024
1 parent 6d88ed5 commit 2bce7b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
29 changes: 14 additions & 15 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn get_buffer(
buffer_type: BufferType,
start: usize,
max_size: usize,
) -> Result<Option<Bytes>, Status> {
) -> Result<Option<Vec<u8>>, Status> {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
unsafe {
Expand Down Expand Up @@ -163,7 +163,7 @@ pub fn get_map(map_type: MapType) -> Result<Vec<(String, String)>, Status> {
}
}

pub fn get_map_bytes(map_type: MapType) -> Result<Vec<(String, Bytes)>, Status> {
pub fn get_map_bytes(map_type: MapType) -> Result<Vec<(String, Vec<u8>)>, Status> {
unsafe {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
Expand Down Expand Up @@ -250,7 +250,7 @@ pub fn get_map_value(map_type: MapType, key: &str) -> Result<Option<String>, Sta
}
}

pub fn get_map_value_bytes(map_type: MapType, key: &str) -> Result<Option<Bytes>, Status> {
pub fn get_map_value_bytes(map_type: MapType, key: &str) -> Result<Option<Vec<u8>>, Status> {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
unsafe {
Expand Down Expand Up @@ -393,7 +393,7 @@ extern "C" {
) -> Status;
}

pub fn get_property(path: Vec<&str>) -> Result<Option<Bytes>, Status> {
pub fn get_property(path: Vec<&str>) -> Result<Option<Vec<u8>>, Status> {
let serialized_path = utils::serialize_property_path(path);
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
Expand Down Expand Up @@ -457,7 +457,7 @@ extern "C" {
) -> Status;
}

pub fn get_shared_data(key: &str) -> Result<(Option<Bytes>, Option<u32>), Status> {
pub fn get_shared_data(key: &str) -> Result<(Option<Vec<u8>>, Option<u32>), Status> {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
let mut return_cas: u32 = 0;
Expand Down Expand Up @@ -568,7 +568,7 @@ extern "C" {
) -> Status;
}

pub fn dequeue_shared_queue(queue_id: u32) -> Result<Option<Bytes>, Status> {
pub fn dequeue_shared_queue(queue_id: u32) -> Result<Option<Vec<u8>>, Status> {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
unsafe {
Expand Down Expand Up @@ -1029,7 +1029,7 @@ extern "C" {
pub fn call_foreign_function(
function_name: &str,
arguments: Option<&[u8]>,
) -> Result<Option<Bytes>, Status> {
) -> Result<Option<Vec<u8>>, Status> {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
unsafe {
Expand Down Expand Up @@ -1139,18 +1139,17 @@ pub fn increment_metric(metric_id: u32, offset: i64) -> Result<(), Status> {
}

mod utils {
use crate::types::Bytes;
use std::convert::TryFrom;

pub(super) fn serialize_property_path(path: Vec<&str>) -> Bytes {
pub(super) fn serialize_property_path(path: Vec<&str>) -> Vec<u8> {
if path.is_empty() {
return Vec::new();
}
let mut size: usize = 0;
for part in &path {
size += part.len() + 1;
}
let mut bytes: Bytes = Vec::with_capacity(size);
let mut bytes = Vec::with_capacity(size);
for part in &path {
bytes.extend_from_slice(part.as_bytes());
bytes.push(0);
Expand All @@ -1159,12 +1158,12 @@ mod utils {
bytes
}

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

pub(super) fn serialize_map_bytes(map: Vec<(&str, &[u8])>) -> Bytes {
pub(super) fn serialize_map_bytes(map: Vec<(&str, &[u8])>) -> Vec<u8> {
let mut size: usize = 4;
for (name, value) in &map {
size += name.len() + value.len() + 10;
}
let mut bytes: Bytes = Vec::with_capacity(size);
let mut bytes = Vec::with_capacity(size);
bytes.extend_from_slice(&map.len().to_le_bytes());
for (name, value) in &map {
bytes.extend_from_slice(&name.len().to_le_bytes());
Expand Down Expand Up @@ -1223,7 +1222,7 @@ mod utils {
map
}

pub(super) fn deserialize_map_bytes(bytes: &[u8]) -> Vec<(String, Bytes)> {
pub(super) fn deserialize_map_bytes(bytes: &[u8]) -> Vec<(String, Vec<u8>)> {
let mut map = Vec::new();
if bytes.is_empty() {
return map;
Expand Down
58 changes: 29 additions & 29 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ pub trait Context {
hostcalls::get_current_time().unwrap()
}

fn get_property(&self, path: Vec<&str>) -> Option<Bytes> {
fn get_property(&self, path: Vec<&str>) -> Option<Vec<u8>> {
hostcalls::get_property(path).unwrap()
}

fn set_property(&self, path: Vec<&str>, value: Option<&[u8]>) {
hostcalls::set_property(path, value).unwrap()
}

fn get_shared_data(&self, key: &str) -> (Option<Bytes>, Option<u32>) {
fn get_shared_data(&self, key: &str) -> (Option<Vec<u8>>, Option<u32>) {
hostcalls::get_shared_data(key).unwrap()
}

Expand All @@ -50,7 +50,7 @@ pub trait Context {
hostcalls::resolve_shared_queue(vm_id, name).unwrap()
}

fn dequeue_shared_queue(&self, queue_id: u32) -> Result<Option<Bytes>, Status> {
fn dequeue_shared_queue(&self, queue_id: u32) -> Result<Option<Vec<u8>>, Status> {
hostcalls::dequeue_shared_queue(queue_id)
}

Expand Down Expand Up @@ -82,35 +82,35 @@ pub trait Context {
hostcalls::get_map(MapType::HttpCallResponseHeaders).unwrap()
}

fn get_http_call_response_headers_bytes(&self) -> Vec<(String, Bytes)> {
fn get_http_call_response_headers_bytes(&self) -> Vec<(String, Vec<u8>)> {
hostcalls::get_map_bytes(MapType::HttpCallResponseHeaders).unwrap()
}

fn get_http_call_response_header(&self, name: &str) -> Option<String> {
hostcalls::get_map_value(MapType::HttpCallResponseHeaders, name).unwrap()
}

fn get_http_call_response_header_bytes(&self, name: &str) -> Option<Bytes> {
fn get_http_call_response_header_bytes(&self, name: &str) -> Option<Vec<u8>> {
hostcalls::get_map_value_bytes(MapType::HttpCallResponseHeaders, name).unwrap()
}

fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::HttpCallResponseBody, start, max_size).unwrap()
}

fn get_http_call_response_trailers(&self) -> Vec<(String, String)> {
hostcalls::get_map(MapType::HttpCallResponseTrailers).unwrap()
}

fn get_http_call_response_trailers_bytes(&self) -> Vec<(String, Bytes)> {
fn get_http_call_response_trailers_bytes(&self) -> Vec<(String, Vec<u8>)> {
hostcalls::get_map_bytes(MapType::HttpCallResponseTrailers).unwrap()
}

fn get_http_call_response_trailer(&self, name: &str) -> Option<String> {
hostcalls::get_map_value(MapType::HttpCallResponseTrailers, name).unwrap()
}

fn get_http_call_response_trailer_bytes(&self, name: &str) -> Option<Bytes> {
fn get_http_call_response_trailer_bytes(&self, name: &str) -> Option<Vec<u8>> {
hostcalls::get_map_value_bytes(MapType::HttpCallResponseTrailers, name).unwrap()
}

Expand All @@ -135,7 +135,7 @@ pub trait Context {

fn on_grpc_call_response(&mut self, _token_id: u32, _status_code: u32, _response_size: usize) {}

fn get_grpc_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
fn get_grpc_call_response_body(&self, start: usize, max_size: usize) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, start, max_size).unwrap()
}

Expand All @@ -155,11 +155,11 @@ pub trait Context {

fn on_grpc_stream_initial_metadata(&mut self, _token_id: u32, _num_elements: u32) {}

fn get_grpc_stream_initial_metadata(&self) -> Vec<(String, Bytes)> {
fn get_grpc_stream_initial_metadata(&self) -> Vec<(String, Vec<u8>)> {
hostcalls::get_map_bytes(MapType::GrpcReceiveInitialMetadata).unwrap()
}

fn get_grpc_stream_initial_metadata_value(&self, name: &str) -> Option<Bytes> {
fn get_grpc_stream_initial_metadata_value(&self, name: &str) -> Option<Vec<u8>> {
hostcalls::get_map_value_bytes(MapType::GrpcReceiveInitialMetadata, name).unwrap()
}

Expand All @@ -169,17 +169,17 @@ pub trait Context {

fn on_grpc_stream_message(&mut self, _token_id: u32, _message_size: usize) {}

fn get_grpc_stream_message(&mut self, start: usize, max_size: usize) -> Option<Bytes> {
fn get_grpc_stream_message(&mut self, start: usize, max_size: usize) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, start, max_size).unwrap()
}

fn on_grpc_stream_trailing_metadata(&mut self, _token_id: u32, _num_elements: u32) {}

fn get_grpc_stream_trailing_metadata(&self) -> Vec<(String, Bytes)> {
fn get_grpc_stream_trailing_metadata(&self) -> Vec<(String, Vec<u8>)> {
hostcalls::get_map_bytes(MapType::GrpcReceiveTrailingMetadata).unwrap()
}

fn get_grpc_stream_trailing_metadata_value(&self, name: &str) -> Option<Bytes> {
fn get_grpc_stream_trailing_metadata_value(&self, name: &str) -> Option<Vec<u8>> {
hostcalls::get_map_value_bytes(MapType::GrpcReceiveTrailingMetadata, name).unwrap()
}

Expand All @@ -201,7 +201,7 @@ pub trait Context {
&self,
function_name: &str,
arguments: Option<&[u8]>,
) -> Result<Option<Bytes>, Status> {
) -> Result<Option<Vec<u8>>, Status> {
hostcalls::call_foreign_function(function_name, arguments)
}

Expand All @@ -219,15 +219,15 @@ pub trait RootContext: Context {
true
}

fn get_vm_configuration(&self) -> Option<Bytes> {
fn get_vm_configuration(&self) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::VmConfiguration, 0, usize::MAX).unwrap()
}

fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
true
}

fn get_plugin_configuration(&self) -> Option<Bytes> {
fn get_plugin_configuration(&self) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::PluginConfiguration, 0, usize::MAX).unwrap()
}

Expand Down Expand Up @@ -263,7 +263,7 @@ pub trait StreamContext: Context {
Action::Continue
}

fn get_downstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
fn get_downstream_data(&self, start: usize, max_size: usize) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::DownstreamData, start, max_size).unwrap()
}

Expand All @@ -285,7 +285,7 @@ pub trait StreamContext: Context {
Action::Continue
}

fn get_upstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
fn get_upstream_data(&self, start: usize, max_size: usize) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::UpstreamData, start, max_size).unwrap()
}

Expand Down Expand Up @@ -315,7 +315,7 @@ pub trait HttpContext: Context {
hostcalls::get_map(MapType::HttpRequestHeaders).unwrap()
}

fn get_http_request_headers_bytes(&self) -> Vec<(String, Bytes)> {
fn get_http_request_headers_bytes(&self) -> Vec<(String, Vec<u8>)> {
hostcalls::get_map_bytes(MapType::HttpRequestHeaders).unwrap()
}

Expand All @@ -331,7 +331,7 @@ pub trait HttpContext: Context {
hostcalls::get_map_value(MapType::HttpRequestHeaders, name).unwrap()
}

fn get_http_request_header_bytes(&self, name: &str) -> Option<Bytes> {
fn get_http_request_header_bytes(&self, name: &str) -> Option<Vec<u8>> {
hostcalls::get_map_value_bytes(MapType::HttpRequestHeaders, name).unwrap()
}

Expand All @@ -355,7 +355,7 @@ pub trait HttpContext: Context {
Action::Continue
}

fn get_http_request_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
fn get_http_request_body(&self, start: usize, max_size: usize) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::HttpRequestBody, start, max_size).unwrap()
}

Expand All @@ -371,7 +371,7 @@ pub trait HttpContext: Context {
hostcalls::get_map(MapType::HttpRequestTrailers).unwrap()
}

fn get_http_request_trailers_bytes(&self) -> Vec<(String, Bytes)> {
fn get_http_request_trailers_bytes(&self) -> Vec<(String, Vec<u8>)> {
hostcalls::get_map_bytes(MapType::HttpRequestTrailers).unwrap()
}

Expand All @@ -387,7 +387,7 @@ pub trait HttpContext: Context {
hostcalls::get_map_value(MapType::HttpRequestTrailers, name).unwrap()
}

fn get_http_request_trailer_bytes(&self, name: &str) -> Option<Bytes> {
fn get_http_request_trailer_bytes(&self, name: &str) -> Option<Vec<u8>> {
hostcalls::get_map_value_bytes(MapType::HttpRequestTrailers, name).unwrap()
}

Expand Down Expand Up @@ -423,7 +423,7 @@ pub trait HttpContext: Context {
hostcalls::get_map(MapType::HttpResponseHeaders).unwrap()
}

fn get_http_response_headers_bytes(&self) -> Vec<(String, Bytes)> {
fn get_http_response_headers_bytes(&self) -> Vec<(String, Vec<u8>)> {
hostcalls::get_map_bytes(MapType::HttpResponseHeaders).unwrap()
}

Expand All @@ -439,7 +439,7 @@ pub trait HttpContext: Context {
hostcalls::get_map_value(MapType::HttpResponseHeaders, name).unwrap()
}

fn get_http_response_header_bytes(&self, name: &str) -> Option<Bytes> {
fn get_http_response_header_bytes(&self, name: &str) -> Option<Vec<u8>> {
hostcalls::get_map_value_bytes(MapType::HttpResponseHeaders, name).unwrap()
}

Expand All @@ -463,7 +463,7 @@ pub trait HttpContext: Context {
Action::Continue
}

fn get_http_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
fn get_http_response_body(&self, start: usize, max_size: usize) -> Option<Vec<u8>> {
hostcalls::get_buffer(BufferType::HttpResponseBody, start, max_size).unwrap()
}

Expand All @@ -479,7 +479,7 @@ pub trait HttpContext: Context {
hostcalls::get_map(MapType::HttpResponseTrailers).unwrap()
}

fn get_http_response_trailers_bytes(&self) -> Vec<(String, Bytes)> {
fn get_http_response_trailers_bytes(&self) -> Vec<(String, Vec<u8>)> {
hostcalls::get_map_bytes(MapType::HttpResponseTrailers).unwrap()
}

Expand All @@ -495,7 +495,7 @@ pub trait HttpContext: Context {
hostcalls::get_map_value(MapType::HttpResponseTrailers, name).unwrap()
}

fn get_http_response_trailer_bytes(&self, name: &str) -> Option<Bytes> {
fn get_http_response_trailer_bytes(&self, name: &str) -> Option<Vec<u8>> {
hostcalls::get_map_value_bytes(MapType::HttpResponseTrailers, name).unwrap()
}

Expand Down

0 comments on commit 2bce7b9

Please sign in to comment.