1212// limitations under the License.
1313
1414#![ allow( unknown_lints) ]
15+ #![ allow( renamed_and_removed_lints) ]
16+ // remove this after Rust's tool_lints is stabilized
1517
1618extern crate libc;
1719
18- use libc:: { c_char, c_int, c_uint, c_void, int32_t, int64_t, size_t, uint32_t} ;
20+ use libc:: { c_char, c_int, c_uint, c_void, int32_t, int64_t, size_t, uint32_t, uint8_t} ;
21+ use std:: mem;
1922use std:: time:: Duration ;
2023
2124/// The clocks gRPC supports.
@@ -348,6 +351,52 @@ pub struct GrpcMetadataArray {
348351 pub metadata : * mut GrpcMetadata ,
349352}
350353
354+ #[ repr( C ) ]
355+ #[ derive( Clone , Copy ) ]
356+ pub struct GrpcSliceRefCounted {
357+ bytes : * mut uint8_t ,
358+ length : size_t ,
359+ }
360+
361+ #[ repr( C ) ]
362+ #[ derive( Clone , Copy ) ]
363+ pub struct GrpcSliceInlined {
364+ length : uint8_t ,
365+ // TODO: use size_of when it becomes a const function.
366+ #[ cfg( target_pointer_width = "64" ) ]
367+ bytes : [ uint8_t ; 23 ] ,
368+ #[ cfg( target_pointer_width = "32" ) ]
369+ bytes : [ uint8_t ; 11 ] ,
370+ }
371+
372+ #[ repr( C ) ]
373+ #[ derive( Clone , Copy ) ]
374+ pub union GrpcSliceData {
375+ ref_counted : GrpcSliceRefCounted ,
376+ inlined : GrpcSliceInlined ,
377+ }
378+
379+ pub enum GrpcSliceRefCount { }
380+
381+ #[ repr( C ) ]
382+ #[ derive( Clone , Copy ) ]
383+ pub struct GrpcSlice {
384+ ref_count : * mut GrpcSliceRefCount ,
385+ data : GrpcSliceData ,
386+ }
387+
388+ #[ repr( C ) ]
389+ pub union GrpcByteBufferReaderCurrent {
390+ index : c_uint ,
391+ }
392+
393+ #[ repr( C ) ]
394+ pub struct GrpcByteBufferReader {
395+ pub buffer_in : * mut GrpcByteBuffer ,
396+ pub buffer_out : * mut GrpcByteBuffer ,
397+ current : GrpcByteBufferReaderCurrent ,
398+ }
399+
351400pub const GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : uint32_t = 0x0000_0010 ;
352401pub const GRPC_INITIAL_METADATA_WAIT_FOR_READY : uint32_t = 0x0000_0020 ;
353402pub const GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : uint32_t = 0x0000_0040 ;
@@ -356,7 +405,6 @@ pub const GRPC_WRITE_BUFFER_HINT: uint32_t = 0x0000_0001;
356405pub const GRPC_WRITE_NO_COMPRESS : uint32_t = 0x0000_0002 ;
357406
358407pub enum GrpcMetadata { }
359- pub enum GrpcSlice { }
360408pub enum GrpcCallDetails { }
361409pub enum GrpcCompletionQueue { }
362410pub enum GrpcChannel { }
@@ -443,17 +491,33 @@ extern "C" {
443491 ) -> * mut GrpcChannel ;
444492 pub fn grpc_channel_destroy ( channel : * mut GrpcChannel ) ;
445493
494+ pub fn grpc_slice_unref ( slice : GrpcSlice ) ;
495+ pub fn grpc_byte_buffer_length ( buf : * const GrpcByteBuffer ) -> size_t ;
496+ pub fn grpcwrap_slice_length ( slice : * const GrpcSlice ) -> size_t ;
497+ pub fn grpcwrap_slice_raw_offset (
498+ slice : * const GrpcSlice ,
499+ offset : size_t ,
500+ len : * mut size_t ,
501+ ) -> * const c_char ;
502+ pub fn grpc_byte_buffer_reader_init (
503+ reader : * mut GrpcByteBufferReader ,
504+ buf : * mut GrpcByteBuffer ,
505+ ) -> c_int ;
506+ pub fn grpc_byte_buffer_reader_next (
507+ reader : * mut GrpcByteBufferReader ,
508+ buf : * mut GrpcSlice ,
509+ ) -> c_int ;
510+ pub fn grpc_byte_buffer_reader_destroy ( reader : * mut GrpcByteBufferReader ) ;
511+ pub fn grpc_byte_buffer_destroy ( buf : * mut GrpcByteBuffer ) ;
512+
446513 pub fn grpcwrap_batch_context_create ( ) -> * mut GrpcBatchContext ;
447514 pub fn grpcwrap_batch_context_destroy ( ctx : * mut GrpcBatchContext ) ;
448515 pub fn grpcwrap_batch_context_recv_initial_metadata (
449516 ctx : * mut GrpcBatchContext ,
450517 ) -> * const GrpcMetadataArray ;
451- pub fn grpcwrap_batch_context_recv_message_length ( ctx : * mut GrpcBatchContext ) -> size_t ;
452- pub fn grpcwrap_batch_context_recv_message_to_buffer (
518+ pub fn grpcwrap_batch_context_take_recv_message (
453519 ctx : * mut GrpcBatchContext ,
454- buffer : * mut c_char ,
455- buffer_len : size_t ,
456- ) ;
520+ ) -> * mut GrpcByteBuffer ;
457521 pub fn grpcwrap_batch_context_recv_status_on_client_status (
458522 ctx : * mut GrpcBatchContext ,
459523 ) -> GrpcStatusCode ;
@@ -476,7 +540,7 @@ extern "C" {
476540 pub fn grpcwrap_call_start_unary (
477541 call : * mut GrpcCall ,
478542 ctx : * mut GrpcBatchContext ,
479- send_bufer : * const c_char ,
543+ send_buffer : * const c_char ,
480544 send_buffer_len : size_t ,
481545 write_flags : uint32_t ,
482546 initial_metadata : * mut GrpcMetadataArray ,
@@ -493,7 +557,7 @@ extern "C" {
493557 pub fn grpcwrap_call_start_server_streaming (
494558 call : * mut GrpcCall ,
495559 ctx : * mut GrpcBatchContext ,
496- send_bufer : * const c_char ,
560+ send_buffer : * const c_char ,
497561 send_buffer_len : size_t ,
498562 write_flags : uint32_t ,
499563 initial_metadata : * mut GrpcMetadataArray ,
@@ -515,7 +579,7 @@ extern "C" {
515579 pub fn grpcwrap_call_send_message (
516580 call : * mut GrpcCall ,
517581 ctx : * mut GrpcBatchContext ,
518- send_bufer : * const c_char ,
582+ send_buffer : * const c_char ,
519583 send_buffer_len : size_t ,
520584 write_flags : uint32_t ,
521585 send_empty_initial_metadata : uint32_t ,
@@ -570,7 +634,7 @@ extern "C" {
570634 server : * mut GrpcServer ,
571635 method : * const c_char ,
572636 host : * const c_char ,
573- paylod_handling : GrpcServerRegisterMethodPayloadHandling ,
637+ payload_handling : GrpcServerRegisterMethodPayloadHandling ,
574638 flags : uint32_t ,
575639 ) -> * mut c_void ;
576640 pub fn grpc_server_create (
@@ -646,6 +710,19 @@ extern "C" {
646710 pub fn grpcwrap_metadata_array_cleanup ( array : * mut GrpcMetadataArray ) ;
647711
648712 pub fn gpr_free ( p : * mut c_void ) ;
713+
714+ pub fn grpcwrap_sanity_check_slice ( size : size_t , align : size_t ) ;
715+ pub fn grpcwrap_sanity_check_byte_buffer_reader ( size : size_t , align : size_t ) ;
716+ }
717+
718+ /// Make sure the complicated struct written in rust is the same with
719+ /// its C one.
720+ pub unsafe fn sanity_check ( ) {
721+ grpcwrap_sanity_check_slice ( mem:: size_of :: < GrpcSlice > ( ) , mem:: align_of :: < GrpcSlice > ( ) ) ;
722+ grpcwrap_sanity_check_byte_buffer_reader (
723+ mem:: size_of :: < GrpcByteBufferReader > ( ) ,
724+ mem:: align_of :: < GrpcByteBufferReader > ( ) ,
725+ ) ;
649726}
650727
651728#[ cfg( feature = "secure" ) ]
@@ -655,6 +732,7 @@ mod secure_component {
655732 use super :: { GrpcChannel , GrpcChannelArgs , GrpcServer } ;
656733
657734 pub enum GrpcChannelCredentials { }
735+
658736 pub enum GrpcServerCredentials { }
659737
660738 extern "C" {
@@ -703,6 +781,7 @@ mod tests {
703781 fn smoke ( ) {
704782 unsafe {
705783 super :: grpc_init ( ) ;
784+ super :: sanity_check ( ) ;
706785 let cq = super :: grpc_completion_queue_create_for_next ( ptr:: null_mut ( ) ) ;
707786 super :: grpc_completion_queue_destroy ( cq) ;
708787 super :: grpc_shutdown ( ) ;
0 commit comments