aboutsummaryrefslogtreecommitdiff
path: root/src/metadata.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/metadata.rs')
-rw-r--r--src/metadata.rs32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/metadata.rs b/src/metadata.rs
index ef49bcb..746b593 100644
--- a/src/metadata.rs
+++ b/src/metadata.rs
@@ -1,7 +1,8 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
-use crate::grpc_sys::{self, grpc_metadata_array};
+use crate::grpc_sys::{self, grpc_metadata, grpc_metadata_array};
use std::borrow::Cow;
+use std::mem::ManuallyDrop;
use std::{mem, slice, str};
use crate::error::{Error, Result};
@@ -184,6 +185,35 @@ impl Metadata {
index: 0,
}
}
+
+ /// Decomposes a Metadata array into its raw components.
+ ///
+ /// Returns the raw pointer to the underlying data, the length of the vector (in elements),
+ /// and the allocated capacity of the data (in elements). These are the same arguments in
+ /// the same order as the arguments to from_raw_parts.
+ ///
+ /// After calling this function, the caller is responsible for the memory previously managed
+ /// by the Metadata. The only way to do this is to convert the raw pointer, length, and
+ /// capacity back into a Metadata with the from_raw_parts function, allowing the destructor
+ /// to perform the cleanup.
+ pub fn into_raw_parts(self) -> (*mut grpc_metadata, usize, usize) {
+ let s = ManuallyDrop::new(self);
+ (s.0.metadata, s.0.count, s.0.capacity)
+ }
+
+ /// Creates a Metadata directly from the raw components of another vector.
+ ///
+ /// ## Safety
+ ///
+ /// The operation is safe only if the three arguments are returned from `into_raw_parts`
+ /// and only convert once.
+ pub unsafe fn from_raw_parts(p: *mut grpc_metadata, len: usize, cap: usize) -> Metadata {
+ Metadata(grpc_metadata_array {
+ count: len,
+ capacity: cap,
+ metadata: p,
+ })
+ }
}
impl Clone for Metadata {