Skip to content

Commit

Permalink
cf fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo Chen authored and Neo Chen committed May 6, 2024
1 parent 08fd173 commit c0553e4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 223 deletions.
1 change: 1 addition & 0 deletions fb_tonic_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tonic = { workspace = true }

[build-dependencies]
tonic-build = "0.11.0"
flatc-rust = "*"

[[bin]]
name = "server"
Expand Down
16 changes: 16 additions & 0 deletions fb_tonic_demo/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
use std::io::Result;
use std::path::Path;

fn main() -> Result<()> {
if let Ok(flatc_bin) = std::env::var("FLATC") {
std::env::set_var("PATH", std::env::var("PATH").unwrap() + format!(":{}", flatc_bin.as_str()).as_str());
}
else {
println!("FLATC enviroment variable not found!");
}

println!("cargo:rerun-if-changed=src/util/fbgreeting.fbs");
flatc_rust::run(flatc_rust::Args {
inputs: &[Path::new("src/util/fbgreeting.fbs")],
out_dir: Path::new("target/flatbuffers/"),
..Default::default()
})
.expect("flatc");

let greeter_service = tonic_build::manual::Service::builder()
.name("Greeter")
.package("fb.helloworld")
Expand Down
24 changes: 8 additions & 16 deletions fb_tonic_demo/src/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,26 @@ use tonic::{
// items been encoded or decoded shall not have any non static references.
// However flatbuffer related types always have a 'fbb lifetime bound, I found no way to implement
// something like serde do.
pub struct FlatBufferBytes {
data: Vec<u8>,
head: usize,
}
pub struct FlatBufferBytes(Vec<u8>);

impl FlatBufferBytes {
pub fn new(data: Vec<u8>, head: usize) -> Self {
Self { data, head }
}

pub fn valid_slice(&self) -> &[u8] {
&(self.data[self.head..])
pub fn new(data: Vec<u8>) -> Self {
Self(data)
}

pub fn serialize<'buf, T: flatbuffers::Follow<'buf> + 'buf>(
mut builder: flatbuffers::FlatBufferBuilder<'buf>,
root_offset: flatbuffers::WIPOffset<T>,
) -> Self {
builder.finish(root_offset, None);
let (data, head) = builder.collapse();
Self { data, head }
let (mut data, head) = builder.collapse();
Self (data.drain(head..).collect())
}

pub fn deserialize<'buf, T: flatbuffers::Follow<'buf> + flatbuffers::Verifiable + 'buf>(
&'buf self,
) -> Result<T::Inner, Box<dyn std::error::Error>> {
let data = self.valid_slice();
flatbuffers::root::<T>(data).map_err(|x| Box::new(x) as Box<dyn std::error::Error>)
flatbuffers::root::<T>(self.0.as_slice()).map_err(|x| Box::new(x) as Box<dyn std::error::Error>)
}
}

Expand All @@ -53,7 +45,7 @@ impl Encoder for FlatBufferEncoder {
type Error = Status;

fn encode(&mut self, item: Self::Item, buf: &mut EncodeBuf<'_>) -> Result<(), Self::Error> {
buf.put_slice(item.valid_slice());
buf.put_slice(item.0.as_slice());
Ok(())
}
}
Expand All @@ -73,7 +65,7 @@ impl Decoder for FlatBufferDecoder {
buf.reader()
.read_to_end(&mut data)
.map_err(|e| Status::internal(e.to_string()))?;
let item = FlatBufferBytes::new(data, 0);
let item = FlatBufferBytes::new(data);
Ok(Some(item))
}
}
Expand Down
207 changes: 0 additions & 207 deletions fb_tonic_demo/src/util/fbgreeting_generated.rs

This file was deleted.

2 changes: 2 additions & 0 deletions fb_tonic_demo/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod common;
#[allow(non_snake_case)]
#[path = "../../target/flatbuffers/fbgreeting_generated.rs"]
pub mod fbgreeting_generated;

0 comments on commit c0553e4

Please sign in to comment.