Files
core-foundation-rs/boolean.rs
T
Patrick Walton dc1184ebe2 Revamp and modernize the Core Foundation APIs.
This change redoes the way Core Foundation works to make the API
smaller, safer, and easier to use. In particular:

* Deriving custom types is now based on `struct`s, not on making a
  type alias of `CFWrapper`.

* The attempts at type safety for `CFArray` and `CFDictionary` were
  removed as not pulling their weight (IMHO).

* Methods that are really unsafe (in particular `wrap_owned` and
  `wrap_shared`) are now marked `unsafe`.

* `wrap_owned` has been renamed to `wrap_under_create_rule` and
  `wrap_shared` has been renamed to `wrap_under_get_rule`, to match
  Apple's documentation.

* `borrow_ref` and friends have been simplified into
  `as_concrete_TypeRef()` and `as_TypeRef()`.

* Dictionaries, arrays, and sets now assume that their elements are
  Core Foundation objects. The old code tried not to assume that,
  but ended up assuming that anyway in a couple of places.

* `CFString`s can be converted from Rust types with the `FromStr`
  trait. `CFNumber`s can be converted to and from Rust types with
  `ToPrimitive` and `FromPrimitive`, eliminating the duplication of
  functionality inside this module.
2013-11-15 09:46:49 +09:00

80 lines
1.8 KiB
Rust

// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! A Boolean type.
use base::{CFRelease, CFTypeID, TCFType};
pub type Boolean = u32;
struct __CFBoolean;
pub type CFBooleanRef = *__CFBoolean;
/// A Boolean type.
///
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
pub struct CFBoolean {
priv obj: CFBooleanRef,
}
impl Drop for CFBoolean {
#[fixed_stack_segment]
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
impl TCFType<CFBooleanRef> for CFBoolean {
fn as_concrete_TypeRef(&self) -> CFBooleanRef {
self.obj
}
unsafe fn wrap_under_create_rule(obj: CFBooleanRef) -> CFBoolean {
CFBoolean {
obj: obj,
}
}
#[fixed_stack_segment]
#[inline]
fn type_id(_: Option<CFBoolean>) -> CFTypeID {
unsafe {
CFBooleanGetTypeID()
}
}
}
impl CFBoolean {
pub fn true_value() -> CFBoolean {
unsafe {
TCFType::wrap_under_get_rule(kCFBooleanTrue)
}
}
pub fn false_value() -> CFBoolean {
unsafe {
TCFType::wrap_under_get_rule(kCFBooleanFalse)
}
}
}
#[link_args="-framework CoreFoundation"]
#[nolink]
extern {
static kCFBooleanTrue: CFBooleanRef;
static kCFBooleanFalse: CFBooleanRef;
fn CFBooleanGetValue(boolean: CFBooleanRef) -> Boolean;
fn CFBooleanGetTypeID() -> CFTypeID;
}