-
Notifications
You must be signed in to change notification settings - Fork 257
metadata: avoid change metadata ref #566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,7 +147,7 @@ impl MetadataBuilder { | |
/// | ||
/// Metadata value can be ascii string or bytes. They are distinguish by the | ||
/// key suffix, key of bytes value should have suffix '-bin'. | ||
#[repr(C)] | ||
#[repr(transparent)] | ||
pub struct Metadata(grpc_metadata_array); | ||
|
||
impl Metadata { | ||
|
@@ -234,10 +234,6 @@ impl Metadata { | |
} | ||
&[] | ||
} | ||
|
||
pub(crate) fn as_mut_ptr(&mut self) -> *mut grpc_metadata_array { | ||
&mut self.0 as _ | ||
} | ||
} | ||
|
||
impl fmt::Debug for Metadata { | ||
|
@@ -273,6 +269,38 @@ impl Drop for Metadata { | |
unsafe impl Send for Metadata {} | ||
unsafe impl Sync for Metadata {} | ||
|
||
/// A special metadata that only for receiving metadata from remote. | ||
/// | ||
/// gRPC C Core manages metadata internally, it's unsafe to read them unless | ||
/// call is not destroyed. | ||
#[repr(transparent)] | ||
pub struct UnownedMetadata(grpc_metadata_array); | ||
|
||
impl UnownedMetadata { | ||
#[inline] | ||
pub fn empty() -> UnownedMetadata { | ||
unsafe { mem::transmute(Metadata::with_capacity(0)) } | ||
} | ||
#[inline] | ||
pub unsafe fn assume_valid(&self) -> &Metadata { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a convertion. It's more like a as_ref/deref. The returned value is not owned, it just reuses the struct for accessing APIs. That's why a reference is returned. |
||
mem::transmute(self) | ||
} | ||
|
||
pub fn as_mut_ptr(&mut self) -> *mut grpc_metadata_array { | ||
&mut self.0 as _ | ||
} | ||
} | ||
|
||
impl Drop for UnownedMetadata { | ||
#[inline] | ||
fn drop(&mut self) { | ||
unsafe { grpcio_sys::grpcwrap_metadata_array_destroy_metadata_only(&mut self.0) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why free here because I don't see any pointer init There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
unsafe impl Send for UnownedMetadata {} | ||
unsafe impl Sync for UnownedMetadata {} | ||
|
||
/// Immutable metadata iterator | ||
/// | ||
/// This struct is created by the iter method on `Metadata`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seem that all struct with single field using
repr(C)
can be replaced withrepr(transparent)
.