diff options
Diffstat (limited to 'third_party/rust/core-foundation/src/data.rs')
-rw-r--r-- | third_party/rust/core-foundation/src/data.rs | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/third_party/rust/core-foundation/src/data.rs b/third_party/rust/core-foundation/src/data.rs index c510c7434d..9f952199aa 100644 --- a/third_party/rust/core-foundation/src/data.rs +++ b/third_party/rust/core-foundation/src/data.rs @@ -9,18 +9,16 @@ //! Core Foundation byte buffers. -pub use core_foundation_sys::data::*; +use core_foundation_sys::base::kCFAllocatorDefault; use core_foundation_sys::base::CFIndex; -use core_foundation_sys::base::{kCFAllocatorDefault}; +pub use core_foundation_sys::data::*; use std::ops::Deref; use std::slice; use std::sync::Arc; +use crate::base::{CFIndexConvertible, TCFType}; -use base::{CFIndexConvertible, TCFType}; - - -declare_TCFType!{ +declare_TCFType! { /// A byte buffer. CFData, CFDataRef } @@ -28,20 +26,22 @@ impl_TCFType!(CFData, CFDataRef, CFDataGetTypeID); impl_CFTypeDescription!(CFData); impl CFData { - /// Creates a CFData around a copy `buffer` + /// Creates a [`CFData`] around a copy `buffer` pub fn from_buffer(buffer: &[u8]) -> CFData { unsafe { - let data_ref = CFDataCreate(kCFAllocatorDefault, - buffer.as_ptr(), - buffer.len().to_CFIndex()); + let data_ref = CFDataCreate( + kCFAllocatorDefault, + buffer.as_ptr(), + buffer.len().to_CFIndex(), + ); TCFType::wrap_under_create_rule(data_ref) } } - /// Creates a CFData referencing `buffer` without creating a copy + /// Creates a [`CFData`] referencing `buffer` without creating a copy pub fn from_arc<T: AsRef<[u8]> + Sync + Send>(buffer: Arc<T>) -> Self { - use std::os::raw::c_void; use crate::base::{CFAllocator, CFAllocatorContext}; + use std::os::raw::c_void; unsafe { let ptr = (*buffer).as_ref().as_ptr() as *const _; @@ -67,8 +67,12 @@ impl CFData { deallocate: Some(deallocate::<T>), preferredSize: None, }); - let data_ref = - CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, ptr, len, allocator.as_CFTypeRef()); + let data_ref = CFDataCreateWithBytesNoCopy( + kCFAllocatorDefault, + ptr, + len, + allocator.as_CFTypeRef(), + ); TCFType::wrap_under_create_rule(data_ref) } } @@ -76,18 +80,20 @@ impl CFData { /// Returns a pointer to the underlying bytes in this data. Note that this byte buffer is /// read-only. #[inline] - pub fn bytes<'a>(&'a self) -> &'a [u8] { - unsafe { - slice::from_raw_parts(CFDataGetBytePtr(self.0), self.len() as usize) - } + pub fn bytes(&self) -> &[u8] { + unsafe { slice::from_raw_parts(CFDataGetBytePtr(self.0), self.len() as usize) } } /// Returns the length of this byte buffer. #[inline] pub fn len(&self) -> CFIndex { - unsafe { - CFDataGetLength(self.0) - } + unsafe { CFDataGetLength(self.0) } + } + + /// Returns `true` if this byte buffer is empty. + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 } } @@ -133,7 +139,10 @@ mod test { } let dropped = Arc::new(AtomicBool::default()); - let l = Arc::new(VecWrapper {inner: vec![5], dropped: dropped.clone() }); + let l = Arc::new(VecWrapper { + inner: vec![5], + dropped: dropped.clone(), + }); let m = l.clone(); let dp = CFData::from_arc(l); drop(m); |