summaryrefslogtreecommitdiffstats
path: root/third_party/rust/core-foundation/src/string.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/core-foundation/src/string.rs')
-rw-r--r--third_party/rust/core-foundation/src/string.rs100
1 files changed, 56 insertions, 44 deletions
diff --git a/third_party/rust/core-foundation/src/string.rs b/third_party/rust/core-foundation/src/string.rs
index 3f5994bc5a..3358bab552 100644
--- a/third_party/rust/core-foundation/src/string.rs
+++ b/third_party/rust/core-foundation/src/string.rs
@@ -11,18 +11,17 @@
pub use core_foundation_sys::string::*;
-use base::{CFIndexConvertible, TCFType};
+use crate::base::{CFIndexConvertible, TCFType};
-use core_foundation_sys::base::{Boolean, CFIndex, CFRange};
use core_foundation_sys::base::{kCFAllocatorDefault, kCFAllocatorNull};
+use core_foundation_sys::base::{Boolean, CFIndex, CFRange};
use std::borrow::Cow;
+use std::ffi::CStr;
use std::fmt;
-use std::str::{self, FromStr};
use std::ptr;
-use std::ffi::CStr;
-
+use std::str::{self, FromStr};
-declare_TCFType!{
+declare_TCFType! {
/// An immutable string in one of a variety of encodings.
CFString, CFStringRef
}
@@ -31,7 +30,7 @@ impl_TCFType!(CFString, CFStringRef, CFStringGetTypeID);
impl FromStr for CFString {
type Err = ();
- /// See also CFString::new for a variant of this which does not return a Result
+ /// See also [`CFString::new()`] for a variant of this which does not return a `Result`.
#[inline]
fn from_str(string: &str) -> Result<CFString, ()> {
Ok(CFString::new(string))
@@ -58,27 +57,37 @@ impl<'a> From<&'a CFString> for Cow<'a, str> {
// First, ask how big the buffer ought to be.
let mut bytes_required: CFIndex = 0;
- CFStringGetBytes(cf_str.0,
- CFRange { location: 0, length: char_len },
- kCFStringEncodingUTF8,
- 0,
- false as Boolean,
- ptr::null_mut(),
- 0,
- &mut bytes_required);
+ CFStringGetBytes(
+ cf_str.0,
+ CFRange {
+ location: 0,
+ length: char_len,
+ },
+ kCFStringEncodingUTF8,
+ 0,
+ false as Boolean,
+ ptr::null_mut(),
+ 0,
+ &mut bytes_required,
+ );
// Then, allocate the buffer and actually copy.
let mut buffer = vec![b'\x00'; bytes_required as usize];
let mut bytes_used: CFIndex = 0;
- let chars_written = CFStringGetBytes(cf_str.0,
- CFRange { location: 0, length: char_len },
- kCFStringEncodingUTF8,
- 0,
- false as Boolean,
- buffer.as_mut_ptr(),
- buffer.len().to_CFIndex(),
- &mut bytes_used);
+ let chars_written = CFStringGetBytes(
+ cf_str.0,
+ CFRange {
+ location: 0,
+ length: char_len,
+ },
+ kCFStringEncodingUTF8,
+ 0,
+ false as Boolean,
+ buffer.as_mut_ptr(),
+ buffer.len().to_CFIndex(),
+ &mut bytes_used,
+ );
assert_eq!(chars_written, char_len);
// This is dangerous; we over-allocate and null-terminate the string (during
@@ -102,17 +111,18 @@ impl fmt::Debug for CFString {
}
}
-
impl CFString {
/// Creates a new `CFString` instance from a Rust string.
#[inline]
pub fn new(string: &str) -> CFString {
unsafe {
- let string_ref = CFStringCreateWithBytes(kCFAllocatorDefault,
- string.as_ptr(),
- string.len().to_CFIndex(),
- kCFStringEncodingUTF8,
- false as Boolean);
+ let string_ref = CFStringCreateWithBytes(
+ kCFAllocatorDefault,
+ string.as_ptr(),
+ string.len().to_CFIndex(),
+ kCFStringEncodingUTF8,
+ false as Boolean,
+ );
CFString::wrap_under_create_rule(string_ref)
}
}
@@ -122,12 +132,14 @@ impl CFString {
#[inline]
pub fn from_static_string(string: &'static str) -> CFString {
unsafe {
- let string_ref = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault,
- string.as_ptr(),
- string.len().to_CFIndex(),
- kCFStringEncodingUTF8,
- false as Boolean,
- kCFAllocatorNull);
+ let string_ref = CFStringCreateWithBytesNoCopy(
+ kCFAllocatorDefault,
+ string.as_ptr(),
+ string.len().to_CFIndex(),
+ kCFStringEncodingUTF8,
+ false as Boolean,
+ kCFAllocatorNull,
+ );
TCFType::wrap_under_create_rule(string_ref)
}
}
@@ -135,21 +147,21 @@ impl CFString {
/// Returns the number of characters in the string.
#[inline]
pub fn char_len(&self) -> CFIndex {
- unsafe {
- CFStringGetLength(self.0)
- }
+ unsafe { CFStringGetLength(self.0) }
}
}
impl<'a> PartialEq<&'a str> for CFString {
fn eq(&self, other: &&str) -> bool {
unsafe {
- let temp = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault,
- other.as_ptr(),
- other.len().to_CFIndex(),
- kCFStringEncodingUTF8,
- false as Boolean,
- kCFAllocatorNull);
+ let temp = CFStringCreateWithBytesNoCopy(
+ kCFAllocatorDefault,
+ other.as_ptr(),
+ other.len().to_CFIndex(),
+ kCFStringEncodingUTF8,
+ false as Boolean,
+ kCFAllocatorNull,
+ );
self.eq(&CFString::wrap_under_create_rule(temp))
}
}