use tonic::codec::DecodeBuf;
struct AdapterDecoder<T>(Vec<T>);
impl<T> AdapterDecoder<T> {
pub fn new(mut items: Vec<T>) -> Self {
items.reverse();
Self(items)
}
}
impl<T> tonic::codec::Decoder for AdapterDecoder<T> {
type Item = T;
type Error = tonic::Status;
fn decode(&mut self, _src: &mut DecodeBuf<'_>) -> Result<Option<Self::Item>, Self::Error> {
Ok(self.0.pop())
}
}
pub fn make_streaming_request<T: Send + Sync + 'static>(
requests: impl IntoIterator<Item = T>,
) -> tonic::Request<tonic::Streaming<T>> {
let items: Vec<T> = requests.into_iter().collect();
let mut body = Vec::new();
for _ in 0..items.len() {
body.push(0u8); body.extend_from_slice(&[0, 0, 0, 0]); }
tonic::Request::new(tonic::Streaming::new_request(
AdapterDecoder::new(items),
String::from_utf8(body).unwrap(),
None,
None,
))
}