Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ build-fuzzer fuzz-target:
###################

gen-all-fbs-rust-code:
for fbs in `find src -name "*.fbs"`; do flatc -r --rust-module-root-file --gen-all -o ./src/hyperlight_common/src/flatbuffers/ $fbs; done
flatc --rust --rust-module-root-file --gen-all -o ./src/hyperlight_common/src/flatbuffers/ ./src/schema/all.fbs
just fmt-apply

install-vcpkg:
Expand Down
6 changes: 0 additions & 6 deletions docs/how-to-use-flatbuffers.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ We recommend building `flatc` from source. To generate rust code, use
```console
just gen-all-fbs-rust-code
```

### Note about generated code

Because we invoke `flatc` multiple times when generating the Rust code, the `mod.rs` generated in `./src/hyperlight_common/src/flatbuffers` is overwritten multiple times and will likely be incorrect. Make sure to manually inspect and if necessary update this file before continuing with your changes as certain modules might be missing. After fixing `mod.rs`, you might need to re-run `just fmt`, since it might not have applied to all generated files if your `mod.rs` was invalid.

>`flatc` does support passing multiple schema files (e.g. it is possible to pass `.\src\schema\*.fbs`), so we could regenerate all the files each time a change was made, however that generates incorrect code (see [here](https://github.com/google/flatbuffers/issues/6800) for details).
398 changes: 309 additions & 89 deletions src/hyperlight_common/src/flatbuffer_wrappers/function_types.rs

Large diffs are not rendered by default.

51 changes: 7 additions & 44 deletions src/hyperlight_common/src/flatbuffer_wrappers/guest_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ limitations under the License.
extern crate flatbuffers;

use alloc::string::{String, ToString};
use alloc::vec::Vec;

use anyhow::{Error, Result};
use flatbuffers::size_prefixed_root;
#[cfg(feature = "tracing")]
use tracing::{Span, instrument};

use crate::flatbuffers::hyperlight::generated::{
ErrorCode as FbErrorCode, GuestError as FbGuestError, GuestErrorArgs,
};
use crate::flatbuffers::hyperlight::generated::ErrorCode as FbErrorCode;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[repr(C)]
Expand All @@ -48,6 +43,7 @@ pub enum ErrorCode {
GuestFunctionParameterTypeMismatch = 14,
GuestError = 15,
ArrayLengthParamIsMissing = 16,
HostFunctionError = 17,
}

impl From<ErrorCode> for FbErrorCode {
Expand All @@ -73,6 +69,7 @@ impl From<ErrorCode> for FbErrorCode {
}
ErrorCode::GuestError => Self::GuestError,
ErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
ErrorCode::HostFunctionError => Self::HostError,
}
}
}
Expand All @@ -99,6 +96,7 @@ impl From<FbErrorCode> for ErrorCode {
}
FbErrorCode::GuestError => Self::GuestError,
FbErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
FbErrorCode::HostError => Self::HostFunctionError,
_ => Self::UnknownError,
}
}
Expand All @@ -123,6 +121,7 @@ impl From<u64> for ErrorCode {
14 => Self::GuestFunctionParameterTypeMismatch,
15 => Self::GuestError,
16 => Self::ArrayLengthParamIsMissing,
17 => Self::HostFunctionError,
_ => Self::UnknownError,
}
}
Expand All @@ -147,6 +146,7 @@ impl From<ErrorCode> for u64 {
ErrorCode::GuestFunctionParameterTypeMismatch => 14,
ErrorCode::GuestError => 15,
ErrorCode::ArrayLengthParamIsMissing => 16,
ErrorCode::HostFunctionError => 17,
}
}
}
Expand Down Expand Up @@ -174,6 +174,7 @@ impl From<ErrorCode> for String {
}
ErrorCode::GuestError => "GuestError".to_string(),
ErrorCode::ArrayLengthParamIsMissing => "ArrayLengthParamIsMissing".to_string(),
ErrorCode::HostFunctionError => "HostFunctionError".to_string(),
}
}
}
Expand All @@ -194,44 +195,6 @@ impl GuestError {
}
}

impl TryFrom<&[u8]> for GuestError {
type Error = Error;
fn try_from(value: &[u8]) -> Result<Self> {
let guest_error_fb = size_prefixed_root::<FbGuestError>(value)
.map_err(|e| anyhow::anyhow!("Error while reading GuestError: {:?}", e))?;
let code = guest_error_fb.code();
let message = match guest_error_fb.message() {
Some(message) => message.to_string(),
None => String::new(),
};
Ok(Self {
code: code.into(),
message,
})
}
}

impl TryFrom<&GuestError> for Vec<u8> {
type Error = Error;
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
fn try_from(value: &GuestError) -> Result<Vec<u8>> {
let mut builder = flatbuffers::FlatBufferBuilder::new();
let message = builder.create_string(&value.message);

let guest_error_fb = FbGuestError::create(
&mut builder,
&GuestErrorArgs {
code: value.code.into(),
message: Some(message),
},
);
builder.finish_size_prefixed(guest_error_fb, None);
let res = builder.finished_data().to_vec();

Ok(res)
}
}

impl Default for GuestError {
#[cfg_attr(feature = "tracing", instrument(parent = Span::current(), level= "Trace"))]
fn default() -> Self {
Expand Down
Loading
Loading