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
24 changes: 24 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Rust

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
working-directory: liquidcan_rust
run: cargo build --verbose
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about also running clippy?

- name: Run tests
working-directory: liquidcan_rust
run: cargo test --verbose
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,19 @@ TSWLatexianTemp*
# option is specified. Footnotes are the stored in a file with suffix Notes.bib.
# Uncomment the next line to have this generated file ignored.
#*Notes.bib

# Rust.gitignore
# Generated by Cargo
# will have compiled files and executables
**/debug/
**/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
**/Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
Empty file removed Rust-Implementation/LiquidCan.rs
Empty file.
13 changes: 13 additions & 0 deletions liquidcan_rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "liquidcan_rust"
version = "0.1.0"
edition = "2024"

[dependencies]
anyhow = "1.0.100"
modular-bitfield = "0.13.0"
static_assertions = "1.1.0"
zerocopy = "0.8.27"
zerocopy-derive = "0.8.27"
liquidcan_rust_macros = { path = "liquidcan_rust_macros" }
liquidcan_rust_macros_derive = { path = "liquidcan_rust_macros/liquidcan_rust_macros_derive" }
9 changes: 9 additions & 0 deletions liquidcan_rust/liquidcan_rust_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "liquidcan_rust_macros"
version = "0.1.0"
edition = "2024"

[dependencies]
paste = "1.0.15"
zerocopy = "0.8.28"
zerocopy-derive = "0.8.28"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "liquidcan_rust_macros_derive"
version = "0.1.0"
edition = "2024"

[lib]
proc-macro = true

[dependencies]
syn = "2.0.110"
quote = "1.0.42"
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::Attribute;

#[proc_macro_derive(EnumDiscriminate)]
pub fn enum_discriminate_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate.
let ast = syn::parse(input).unwrap();

// Build the trait implementation.
impl_enum_discriminate_derive(&ast)
}

fn has_repr_u8(attrs: &[Attribute]) -> bool {
let mut is_u8 = false;
for attr in attrs {
if attr.path().is_ident("repr") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("u8") {
is_u8 = true;
}
Ok(())
})
.unwrap()
}
}
is_u8
}

fn impl_enum_discriminate_derive(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
if !has_repr_u8(&ast.attrs) {
panic!("EnumDiscriminate can only be derived for enums which have the u8 repr");
}
let generated = quote! {
impl #name {
pub const fn discriminant(&self) -> u8 {
// SAFETY: Because we require the enum to be marked as `repr(u8)`, its layout is a `repr(C)` `union`
// between `repr(C)` structs, each of which has the `u8` discriminant as its first
// field, so we can read the discriminant without offsetting the pointer.
unsafe {
let ptr = self as *const Self;
let discriminant_ptr = ptr.cast::<u8>();
*discriminant_ptr
}
}
}
};
generated.into()
}
4 changes: 4 additions & 0 deletions liquidcan_rust/liquidcan_rust_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[doc(hidden)]
pub use paste::paste;

mod padded_enum;
Loading