From 04bc6f11ae0289aae57d63d4cd32f4ef305d1a7a Mon Sep 17 00:00:00 2001 From: KyleMiles Date: Thu, 29 Jun 2023 20:49:26 -0400 Subject: Move binary view loading in to the core; deprecate open_view in favor of load; update examples --- rust/src/metadata.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 3 deletions(-) (limited to 'rust/src/metadata.rs') diff --git a/rust/src/metadata.rs b/rust/src/metadata.rs index 09c21ec9..1ea8225d 100644 --- a/rust/src/metadata.rs +++ b/rust/src/metadata.rs @@ -379,16 +379,32 @@ impl From for Ref { } } -impl From for Ref { - fn from(value: S) -> Self { +impl From for Ref { + fn from(value: String) -> Self { unsafe { Metadata::ref_from_raw(BNCreateMetadataStringData( - value.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, + value.into_bytes_with_nul().as_ptr() as *const c_char, )) } } } +impl From<&str> for Ref { + fn from(value: &str) -> Self { + unsafe { + Metadata::ref_from_raw(BNCreateMetadataStringData( + value.into_bytes_with_nul().as_ptr() as *const c_char, + )) + } + } +} + +impl>> From<&T> for Ref { + fn from(value: &T) -> Self { + value.into() + } +} + impl From<&Vec> for Ref { fn from(value: &Vec) -> Self { unsafe { Metadata::ref_from_raw(BNCreateMetadataRawData(value.as_ptr(), value.len())) } @@ -442,6 +458,56 @@ impl From>> for Ref { } } +impl>> From<&[(S, T)]> for Ref { + fn from(value: &[(S, T)]) -> Self { + let mut key_refs: Vec = vec![]; + let mut keys: Vec<*const c_char> = vec![]; + let mut values: Vec<*mut BNMetadata> = vec![]; + for (k, v) in value.iter() { + key_refs.push(k.into_bytes_with_nul()); + let value_metadata: Ref = v.into(); + values.push(value_metadata.handle); + } + for k in &key_refs { + keys.push(k.as_ref().as_ptr() as *const c_char); + } + + unsafe { + Metadata::ref_from_raw(BNCreateMetadataValueStore( + keys.as_mut_ptr(), + values.as_mut_ptr(), + keys.len(), + )) + } + } +} + +impl>, const N: usize> From<[(S, T); N]> + for Ref +{ + fn from(value: [(S, T); N]) -> Self { + let mut key_refs: Vec = vec![]; + let mut keys: Vec<*const c_char> = vec![]; + let mut values: Vec<*mut BNMetadata> = vec![]; + for (k, v) in value.into_iter() { + key_refs.push(k.into_bytes_with_nul()); + let value_metadata: Ref = v.into(); + values.push(value_metadata.handle); + } + for k in &key_refs { + keys.push(k.as_ref().as_ptr() as *const c_char); + } + + unsafe { + Metadata::ref_from_raw(BNCreateMetadataValueStore( + keys.as_mut_ptr(), + values.as_mut_ptr(), + keys.len(), + )) + } + } +} + impl From<&Vec> for Ref { fn from(value: &Vec) -> Self { unsafe { -- cgit v1.3.1