mirror of
https://github.com/logos-blockchain/logos-blockchain-circuits.git
synced 2026-05-18 15:29:26 +00:00
Implement FFI types. Add placeholder for native types.
This commit is contained in:
parent
0683b26c7c
commit
acf9d82841
3
.gitignore
vendored
3
.gitignore
vendored
@ -7,3 +7,6 @@ result
|
||||
*.r1cs
|
||||
**/input.json
|
||||
**/*-input.json
|
||||
|
||||
# Rust Codebase
|
||||
rust/**/target/
|
||||
|
||||
16
rust/logos-blockchain-circuits-types/Cargo.lock
generated
Normal file
16
rust/logos-blockchain-circuits-types/Cargo.lock
generated
Normal file
@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.185"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52ff2c0fe9bc6cb6b14a0592c2ff4fa9ceb83eea9db979b0487cd054946a2b8f"
|
||||
|
||||
[[package]]
|
||||
name = "logos-blockchain-circuits-types"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
7
rust/logos-blockchain-circuits-types/Cargo.toml
Normal file
7
rust/logos-blockchain-circuits-types/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "logos-blockchain-circuits-types"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.185"
|
||||
38
rust/logos-blockchain-circuits-types/src/ffi/bytes.rs
Normal file
38
rust/logos-blockchain-circuits-types/src/ffi/bytes.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use libc::free;
|
||||
|
||||
mod inner {
|
||||
#[repr(C)]
|
||||
pub struct Buffer<T> {
|
||||
pub data: T,
|
||||
pub size: usize,
|
||||
}
|
||||
}
|
||||
|
||||
pub type Bytes = inner::Buffer<*mut u8>;
|
||||
pub type ConstBytes = inner::Buffer<*const u8>;
|
||||
|
||||
/// Frees the data buffer inside a [`Bytes`] struct allocated by the C API.
|
||||
///
|
||||
/// Only the inner data buffer is freed, not the struct itself, since the latter is managed by the
|
||||
/// caller.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// - `bytes`: A pointer to a [`Bytes`] struct whose data buffer was allocated by the C API and
|
||||
/// needs to be freed.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Dereferences raw pointers. The caller must ensure that the pointer is valid.
|
||||
pub unsafe fn free_bytes(bytes: *mut Bytes) {
|
||||
if bytes.is_null() {
|
||||
return;
|
||||
}
|
||||
|
||||
let bytes = unsafe { &mut *bytes };
|
||||
if !bytes.data.is_null() {
|
||||
unsafe { free(bytes.data as *mut libc::c_void) };
|
||||
}
|
||||
bytes.data = std::ptr::null_mut();
|
||||
bytes.size = 0;
|
||||
}
|
||||
7
rust/logos-blockchain-circuits-types/src/ffi/mod.rs
Normal file
7
rust/logos-blockchain-circuits-types/src/ffi/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
pub mod status;
|
||||
pub mod bytes;
|
||||
pub mod witness_input;
|
||||
|
||||
pub use status::Status;
|
||||
pub use bytes::{Bytes, ConstBytes, free_bytes};
|
||||
pub use witness_input::WitnessInput;
|
||||
44
rust/logos-blockchain-circuits-types/src/ffi/status.rs
Normal file
44
rust/logos-blockchain-circuits-types/src/ffi/status.rs
Normal file
@ -0,0 +1,44 @@
|
||||
use std::cmp::PartialEq;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Code {
|
||||
Ok = 0,
|
||||
DynError = 1,
|
||||
InvalidInput = 2,
|
||||
OutOfMemory = 3,
|
||||
}
|
||||
|
||||
impl Code {
|
||||
pub fn is_ok(&self) -> bool {
|
||||
self == &Code::Ok
|
||||
}
|
||||
|
||||
pub fn is_error(&self) -> bool {
|
||||
!self.is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct Status {
|
||||
pub code: Code,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl Status {
|
||||
pub fn ok() -> Self {
|
||||
Status {
|
||||
code: Code::Ok,
|
||||
message: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_ok(&self) -> bool {
|
||||
self.code.is_ok()
|
||||
}
|
||||
|
||||
pub fn is_error(&self) -> bool {
|
||||
self.code.is_error()
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
use std::ffi::c_char;
|
||||
use crate::ffi::ConstBytes;
|
||||
|
||||
pub struct WitnessInput {
|
||||
pub dat: ConstBytes,
|
||||
pub inputs_json: *const c_char,
|
||||
}
|
||||
2
rust/logos-blockchain-circuits-types/src/lib.rs
Normal file
2
rust/logos-blockchain-circuits-types/src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod ffi;
|
||||
pub mod types;
|
||||
3
rust/logos-blockchain-circuits-types/src/types/mod.rs
Normal file
3
rust/logos-blockchain-circuits-types/src/types/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod bytes;
|
||||
pub mod status;
|
||||
pub mod witness_input;
|
||||
Loading…
x
Reference in New Issue
Block a user