-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor upb/rust directory to introduce separate files for each conc…
…ept instead of a single blob. PiperOrigin-RevId: 625333833
- Loading branch information
1 parent
55696ad
commit d44ba90
Showing
11 changed files
with
343 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
Oops, something went wrong.