proxy_sdk/
lib.rs

1#![allow(clippy::module_inception)]
2use std::{
3    mem::MaybeUninit,
4    ops::{Bound, RangeBounds},
5};
6
7#[doc = include_str!("../README.md")]
8use log::warn;
9
10mod hostcalls;
11pub use hostcalls::call_foreign_function;
12
13mod status;
14pub use status::*;
15
16mod dispatcher;
17pub use dispatcher::set_root_context_factory;
18
19mod context;
20pub use context::*;
21
22mod http_call;
23pub use http_call::*;
24
25mod grpc_call;
26pub use grpc_call::*;
27
28mod grpc_stream;
29pub use grpc_stream::*;
30
31mod http;
32pub use http::*;
33
34mod queue;
35pub use queue::Queue;
36
37mod shared_data;
38pub use shared_data::SharedData;
39
40pub mod property;
41
42mod envoy;
43
44mod stream;
45pub use stream::*;
46
47mod upstream;
48pub use upstream::Upstream;
49
50mod metrics;
51pub use metrics::*;
52
53mod logger;
54pub use logger::set_log_level;
55
56#[cfg(target_arch = "wasm32")]
57mod rng;
58
59pub mod env;
60
61mod time;
62pub use time::*;
63
64mod downcast_box;
65
66#[cfg(not(target_arch = "wasm32"))]
67pub mod native;
68
69#[no_mangle]
70pub extern "C" fn proxy_abi_version_0_2_1() {}
71
72#[cfg_attr(target_arch = "wasm32", export_name = "malloc")]
73#[no_mangle]
74pub extern "C" fn proxy_on_memory_allocate(size: usize) -> *mut u8 {
75    let mut vec: Vec<MaybeUninit<u8>> = Vec::with_capacity(size);
76    unsafe {
77        vec.set_len(size);
78    }
79    let slice = vec.into_boxed_slice();
80    Box::into_raw(slice) as *mut u8
81}
82
83/// Wipes all thread local state, to be used before any initialization in case of VM reuse in native mode
84pub fn reset() {
85    dispatcher::reset();
86}
87
88pub(crate) fn log_concern<T: Default>(context: &str, result: Result<T, Status>) -> T {
89    match result {
90        Ok(x) => x,
91        Err(e) => {
92            warn!("[concern-{context}] {e:?}");
93            T::default()
94        }
95    }
96}
97
98pub(crate) fn check_concern<T>(context: &str, result: Result<T, Status>) -> Option<T> {
99    match result {
100        Ok(x) => Some(x),
101        Err(e) => {
102            warn!("[concern-{context}] {e:?}");
103            None
104        }
105    }
106}
107
108pub(crate) fn calculate_range(range: impl RangeBounds<usize>, limit: usize) -> (usize, usize) {
109    let start = match range.start_bound() {
110        Bound::Included(x) => *x,
111        Bound::Excluded(x) => x.saturating_sub(1),
112        Bound::Unbounded => 0,
113    };
114    let size = match range.end_bound() {
115        Bound::Included(x) => *x + 1,
116        Bound::Excluded(x) => *x,
117        Bound::Unbounded => limit,
118    }
119    .min(limit)
120    .saturating_sub(start);
121    (start, size)
122}