Skip to content

Commit

Permalink
Refactor upb/rust directory to introduce separate files for each conc…
Browse files Browse the repository at this point in the history
…ept instead of a single blob.

PiperOrigin-RevId: 625333833
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Apr 16, 2024
1 parent 55696ad commit d44ba90
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 287 deletions.
9 changes: 9 additions & 0 deletions rust/upb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ rust_library(
name = "upb",
srcs = [
"arena.rs",
"array.rs",
"ctype.rs",
"extension_registry.rs",
"lib.rs",
"map.rs",
"message.rs",
"message_value.rs",
"mini_table.rs",
"opaque_pointee.rs",
"string_view.rs",
"wire.rs",
],
visibility = [
"//rust:__subpackages__",
Expand Down
42 changes: 42 additions & 0 deletions rust/upb/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::opaque_pointee::opaque_pointee;
use crate::{upb_MessageValue, upb_MutableMessageValue, CType, RawArena};
use std::ptr::NonNull;

opaque_pointee!(upb_Array);
pub type RawArray = NonNull<upb_Array>;

extern "C" {
pub fn upb_Array_New(a: RawArena, r#type: CType) -> RawArray;
pub fn upb_Array_Size(arr: RawArray) -> usize;
pub fn upb_Array_Set(arr: RawArray, i: usize, val: upb_MessageValue);
pub fn upb_Array_Get(arr: RawArray, i: usize) -> upb_MessageValue;
pub fn upb_Array_Append(arr: RawArray, val: upb_MessageValue, arena: RawArena) -> bool;
pub fn upb_Array_Resize(arr: RawArray, size: usize, arena: RawArena) -> bool;
pub fn upb_Array_MutableDataPtr(arr: RawArray) -> *mut std::ffi::c_void;
pub fn upb_Array_DataPtr(arr: RawArray) -> *const std::ffi::c_void;
pub fn upb_Array_GetMutable(arr: RawArray, i: usize) -> upb_MutableMessageValue;
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn array_ffi_test() {
// SAFETY: FFI unit test uses C API under expected patterns.
unsafe {
let arena = crate::Arena::new();
let raw_arena = arena.raw();
let array = upb_Array_New(raw_arena, CType::Float);

assert!(upb_Array_Append(array, upb_MessageValue { float_val: 7.0 }, raw_arena));
assert!(upb_Array_Append(array, upb_MessageValue { float_val: 42.0 }, raw_arena));
assert_eq!(upb_Array_Size(array), 2);
assert!(matches!(upb_Array_Get(array, 1), upb_MessageValue { float_val: 42.0 }));

assert!(upb_Array_Resize(array, 3, raw_arena));
assert_eq!(upb_Array_Size(array), 3);
assert!(matches!(upb_Array_Get(array, 2), upb_MessageValue { float_val: 0.0 }));
}
}
}
17 changes: 17 additions & 0 deletions rust/upb/ctype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Transcribed from google3/third_party/upb/upb/base/descriptor_constants.h
#[repr(C)]
#[allow(dead_code)]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum CType {
Bool = 1,
Float = 2,
Int32 = 3,
UInt32 = 4,
Enum = 5,
Message = 6,
Double = 7,
Int64 = 8,
UInt64 = 9,
String = 10,
Bytes = 11,
}
5 changes: 5 additions & 0 deletions rust/upb/extension_registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::opaque_pointee::opaque_pointee;
use std::ptr::NonNull;

opaque_pointee!(upb_ExtensionRegistry);
pub type RawExtensionRegistry = NonNull<upb_ExtensionRegistry>;
Loading

0 comments on commit d44ba90

Please sign in to comment.