tonic-build: option to emit Arc<Self> as server receiver (#1352)
This implements an option to emit `Arc<Self>` instead of `&self` in server traits. This enables implementor to reference `Self` data for indefinite duration, which may be useful for streaming requests. Fixes: #1351
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
authors = ["Aurimas Blažulionis <[email protected]>"]
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
name = "use_arc_self"
|
||||
publish = false
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3"
|
||||
prost = "0.11"
|
||||
tonic = {path = "../../tonic", features = ["gzip"]}
|
||||
|
||||
[build-dependencies]
|
||||
tonic-build = {path = "../../tonic-build" }
|
||||
|
||||
[package.metadata.cargo-machete]
|
||||
ignored = ["prost"]
|
||||
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2020 Lucio Franco
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,6 @@
|
||||
fn main() {
|
||||
tonic_build::configure()
|
||||
.use_arc_self(true)
|
||||
.compile(&["proto/test.proto"], &["proto"])
|
||||
.unwrap();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package test;
|
||||
|
||||
service Test {
|
||||
rpc TestRequest(SomeData) returns (SomeData);
|
||||
}
|
||||
|
||||
message SomeData {
|
||||
// include a bunch of data so there actually is something to compress
|
||||
bytes data = 1;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#![allow(unused_imports)]
|
||||
|
||||
use futures::{Stream, StreamExt};
|
||||
use std::sync::Arc;
|
||||
use tonic::{Request, Response, Status};
|
||||
|
||||
tonic::include_proto!("test");
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Svc;
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl test_server::Test for Svc {
|
||||
async fn test_request(
|
||||
self: Arc<Self>,
|
||||
req: Request<SomeData>,
|
||||
) -> Result<Response<SomeData>, Status> {
|
||||
Ok(Response::new(req.into_inner()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user