feat: gate alloc/std features for no_std/no_alloc support
Gate generated code behind feature flags so consumers can build in no_std/no_alloc environments: - Runtime: gate RotoMessage, RotoOwned, BufMutBuilder behind alloc - Codegen: split DATA_IMPORTS, gate Owned* structs + service code - Fix RevBuilder::with() to iterate forward without Vec allocation - Wire up bytes/std via alloc feature (Bytes requires Arc) - Update consumer Cargo.toml files with proper feature chains - Add no_std_test example for embedded validation
This commit is contained in:
@@ -21,5 +21,6 @@ http = "1.1"
|
||||
tonic-build = "0.12"
|
||||
|
||||
[features]
|
||||
default = ["alloc"]
|
||||
alloc = []
|
||||
default = ["std"]
|
||||
std = ["roto-runtime/std", "alloc"]
|
||||
alloc = ["roto-runtime/alloc"]
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
// @generated by protoc-gen-roto — do not edit
|
||||
#[allow(unused, unused_imports, unused_assignments, unused_variables, non_camel_case_types)]
|
||||
use roto_runtime::{ProtoAccessor, ProtoBuilder, RevBuilder, Result, RotoError, read_varint, RepeatedFieldIterator, RotoMessage};
|
||||
use core::str;
|
||||
use bytes::{Bytes, BytesMut, Buf, BufMut};
|
||||
|
||||
#[allow(unused, unused_imports, unused_assignments, unused_variables, non_camel_case_types)]use roto_runtime::{ProtoAccessor, ProtoBuilder, RevBuilder, Result, RotoError, read_varint, RepeatedFieldIterator};use core::str;use bytes::Buf;#[cfg(feature = "alloc")]use bytes::{Bytes, BytesMut, BufMut};#[cfg(feature = "alloc")]use roto_runtime::RotoMessage;
|
||||
pub struct UnaryRequest<'a> {
|
||||
accessor: roto_runtime::ProtoAccessor<'a>,
|
||||
message_offset: Option<usize>,
|
||||
@@ -100,8 +96,7 @@ impl<'b> UnaryRequestRevBuilder<'b> {
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &UnaryRequest<'_>) -> roto_runtime::Result<Self> {
|
||||
let fields: Vec<_> = msg.accessor.raw_fields().collect();
|
||||
for item in fields.into_iter().rev() {
|
||||
for item in msg.accessor.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
1 => self.message_written,
|
||||
@@ -119,27 +114,13 @@ impl<'b> UnaryRequestRevBuilder<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OwnedUnaryRequest {
|
||||
pub data: bytes::Bytes,
|
||||
}
|
||||
#[cfg(feature = "alloc")]pub struct OwnedUnaryRequest {pub data: bytes::Bytes,}
|
||||
|
||||
impl roto_runtime::RotoOwned for OwnedUnaryRequest {
|
||||
type Reader<'a> = UnaryRequest<'a>;
|
||||
fn reader(&self) -> UnaryRequest<'_> {
|
||||
UnaryRequest::new(&self.data).expect("failed to create reader")
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "alloc")]impl roto_runtime::RotoOwned for OwnedUnaryRequest {type Reader<'a> = UnaryRequest<'a>;fn reader(&self) -> UnaryRequest<'_> {UnaryRequest::new(&self.data).expect("failed to create reader")}}
|
||||
|
||||
impl roto_runtime::RotoMessage for OwnedUnaryRequest {
|
||||
fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
|
||||
Ok(OwnedUnaryRequest { data: buf })
|
||||
}
|
||||
|
||||
fn bytes(&self) -> bytes::Bytes {
|
||||
self.data.clone()
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "alloc")]impl roto_runtime::RotoMessage for OwnedUnaryRequest {fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {Ok(OwnedUnaryRequest { data: buf })}
|
||||
|
||||
fn bytes(&self) -> bytes::Bytes {self.data.clone()}}
|
||||
pub struct UnaryResponse<'a> {
|
||||
accessor: roto_runtime::ProtoAccessor<'a>,
|
||||
reply_offset: Option<usize>,
|
||||
@@ -236,8 +217,7 @@ impl<'b> UnaryResponseRevBuilder<'b> {
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &UnaryResponse<'_>) -> roto_runtime::Result<Self> {
|
||||
let fields: Vec<_> = msg.accessor.raw_fields().collect();
|
||||
for item in fields.into_iter().rev() {
|
||||
for item in msg.accessor.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
1 => self.reply_written,
|
||||
@@ -255,27 +235,13 @@ impl<'b> UnaryResponseRevBuilder<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OwnedUnaryResponse {
|
||||
pub data: bytes::Bytes,
|
||||
}
|
||||
#[cfg(feature = "alloc")]pub struct OwnedUnaryResponse {pub data: bytes::Bytes,}
|
||||
|
||||
impl roto_runtime::RotoOwned for OwnedUnaryResponse {
|
||||
type Reader<'a> = UnaryResponse<'a>;
|
||||
fn reader(&self) -> UnaryResponse<'_> {
|
||||
UnaryResponse::new(&self.data).expect("failed to create reader")
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "alloc")]impl roto_runtime::RotoOwned for OwnedUnaryResponse {type Reader<'a> = UnaryResponse<'a>;fn reader(&self) -> UnaryResponse<'_> {UnaryResponse::new(&self.data).expect("failed to create reader")}}
|
||||
|
||||
impl roto_runtime::RotoMessage for OwnedUnaryResponse {
|
||||
fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
|
||||
Ok(OwnedUnaryResponse { data: buf })
|
||||
}
|
||||
|
||||
fn bytes(&self) -> bytes::Bytes {
|
||||
self.data.clone()
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "alloc")]impl roto_runtime::RotoMessage for OwnedUnaryResponse {fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {Ok(OwnedUnaryResponse { data: buf })}
|
||||
|
||||
fn bytes(&self) -> bytes::Bytes {self.data.clone()}}
|
||||
pub struct StreamingRequest<'a> {
|
||||
accessor: roto_runtime::ProtoAccessor<'a>,
|
||||
query_offset: Option<usize>,
|
||||
@@ -372,8 +338,7 @@ impl<'b> StreamingRequestRevBuilder<'b> {
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &StreamingRequest<'_>) -> roto_runtime::Result<Self> {
|
||||
let fields: Vec<_> = msg.accessor.raw_fields().collect();
|
||||
for item in fields.into_iter().rev() {
|
||||
for item in msg.accessor.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
1 => self.query_written,
|
||||
@@ -391,27 +356,13 @@ impl<'b> StreamingRequestRevBuilder<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OwnedStreamingRequest {
|
||||
pub data: bytes::Bytes,
|
||||
}
|
||||
#[cfg(feature = "alloc")]pub struct OwnedStreamingRequest {pub data: bytes::Bytes,}
|
||||
|
||||
impl roto_runtime::RotoOwned for OwnedStreamingRequest {
|
||||
type Reader<'a> = StreamingRequest<'a>;
|
||||
fn reader(&self) -> StreamingRequest<'_> {
|
||||
StreamingRequest::new(&self.data).expect("failed to create reader")
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "alloc")]impl roto_runtime::RotoOwned for OwnedStreamingRequest {type Reader<'a> = StreamingRequest<'a>;fn reader(&self) -> StreamingRequest<'_> {StreamingRequest::new(&self.data).expect("failed to create reader")}}
|
||||
|
||||
impl roto_runtime::RotoMessage for OwnedStreamingRequest {
|
||||
fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
|
||||
Ok(OwnedStreamingRequest { data: buf })
|
||||
}
|
||||
|
||||
fn bytes(&self) -> bytes::Bytes {
|
||||
self.data.clone()
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "alloc")]impl roto_runtime::RotoMessage for OwnedStreamingRequest {fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {Ok(OwnedStreamingRequest { data: buf })}
|
||||
|
||||
fn bytes(&self) -> bytes::Bytes {self.data.clone()}}
|
||||
pub struct StreamingResponse<'a> {
|
||||
accessor: roto_runtime::ProtoAccessor<'a>,
|
||||
item_offset: Option<usize>,
|
||||
@@ -508,8 +459,7 @@ impl<'b> StreamingResponseRevBuilder<'b> {
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &StreamingResponse<'_>) -> roto_runtime::Result<Self> {
|
||||
let fields: Vec<_> = msg.accessor.raw_fields().collect();
|
||||
for item in fields.into_iter().rev() {
|
||||
for item in msg.accessor.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
1 => self.item_written,
|
||||
@@ -527,65 +477,67 @@ impl<'b> StreamingResponseRevBuilder<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OwnedStreamingResponse {
|
||||
pub data: bytes::Bytes,
|
||||
}
|
||||
#[cfg(feature = "alloc")]pub struct OwnedStreamingResponse {pub data: bytes::Bytes,}
|
||||
|
||||
impl roto_runtime::RotoOwned for OwnedStreamingResponse {
|
||||
type Reader<'a> = StreamingResponse<'a>;
|
||||
fn reader(&self) -> StreamingResponse<'_> {
|
||||
StreamingResponse::new(&self.data).expect("failed to create reader")
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "alloc")]impl roto_runtime::RotoOwned for OwnedStreamingResponse {type Reader<'a> = StreamingResponse<'a>;fn reader(&self) -> StreamingResponse<'_> {StreamingResponse::new(&self.data).expect("failed to create reader")}}
|
||||
|
||||
impl roto_runtime::RotoMessage for OwnedStreamingResponse {
|
||||
fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
|
||||
Ok(OwnedStreamingResponse { data: buf })
|
||||
}
|
||||
#[cfg(feature = "alloc")]impl roto_runtime::RotoMessage for OwnedStreamingResponse {fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {Ok(OwnedStreamingResponse { data: buf })}
|
||||
|
||||
fn bytes(&self) -> bytes::Bytes {
|
||||
self.data.clone()
|
||||
}
|
||||
}
|
||||
fn bytes(&self) -> bytes::Bytes {self.data.clone()}}
|
||||
|
||||
|
||||
|
||||
#[allow(unused, unused_imports, unused_assignments, unused_variables, non_camel_case_types)]
|
||||
#[cfg(feature = "std")]
|
||||
use tonic::{Request, Response, Status};
|
||||
#[cfg(feature = "std")]
|
||||
use tokio_stream::Stream;
|
||||
#[cfg(feature = "std")]
|
||||
use std::pin::Pin;
|
||||
#[cfg(feature = "std")]
|
||||
use std::sync::Arc;
|
||||
#[cfg(feature = "std")]
|
||||
use std::task::{Context, Poll};
|
||||
#[cfg(feature = "std")]
|
||||
use std::future::Future;
|
||||
#[cfg(feature = "std")]
|
||||
use tonic::body::BoxBody;
|
||||
#[cfg(feature = "std")]
|
||||
use tower::Service;
|
||||
#[cfg(feature = "std")]
|
||||
use futures_util::StreamExt;
|
||||
#[cfg(feature = "std")]
|
||||
use http_body_util::BodyExt;
|
||||
#[cfg(feature = "std")]
|
||||
use http_body::Body;
|
||||
#[cfg(feature = "std")]
|
||||
use crate::{BufferPool, StatusBody};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[async_trait::async_trait]
|
||||
pub trait InteropService: Send + Sync + 'static {
|
||||
async fn unary_call(&self, request: Request<OwnedUnaryRequest>) -> std::result::Result<Response<OwnedUnaryResponse>, Status>;
|
||||
async fn streaming_call(&self, request: Request<OwnedStreamingRequest>) -> std::result::Result<Response<Pin<Box<dyn Stream<Item = std::result::Result<OwnedStreamingResponse, Status>> + Send>>>, Status>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[derive(Clone)]
|
||||
pub struct InteropServiceServer {
|
||||
inner: Arc<dyn InteropService>,
|
||||
pool: Arc<BufferPool>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl InteropServiceServer {
|
||||
pub fn new(inner: Arc<dyn InteropService>, pool: Arc<BufferPool>) -> Self {
|
||||
Self { inner, pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl tonic::server::NamedService for InteropServiceServer {
|
||||
const NAME: &'static str = "interop.InteropService";
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Service<http::Request<BoxBody>> for InteropServiceServer {
|
||||
type Response = http::Response<BoxBody>;
|
||||
type Error = std::convert::Infallible;
|
||||
|
||||
Reference in New Issue
Block a user