Commit Graph

380 Commits

Author SHA1 Message Date
Linus Färnstrand 3aa97e2abe Move CFRunLoopSource method to runloop.rs 2018-01-21 16:33:07 +01:00
bors-servo 8834563793 Auto merge of #142 - faern:add-consuming-cheap-cast, r=jdm
Add consuming cheap cast

There are a lot of places where we increment and decrement the retain count when we would not have to. For example, a very common use-case with PropertyLists is to just directly cast them to their correct sub type and then throw the PropertyList away (because there is nothing you can do with the PropertyList instance directly.)

The reason we do this, I guess, is mostly because all `Drop` implementations force a retain count decrement. So we have to keep incrementing it to keep it correct. But if we add a flag to types that can be used to disable the decrement, then we can also get rid of the increment when we virtually just want to cast a pointer between two types.

This PR implements this by adding a boolean flag to all CF structs that is used by the `Drop` impl to determine if it should `CFRelease` the instance or not. Then this flag is modified in appropriate places where we cast to other types and consume ourselves.

I added some benchmarks locally. I did not want to commit them since benchmarking only works on nighty, and I did not feel like adding a feature flag for it etc. Anyhow, the following benchmarks:
```rust
#[bench]
fn bench_before(b: &mut Bencher) {
   let string = CFString::from_static_string("Bar");
    b.iter(|| unsafe {
        string.clone()
            .to_CFPropertyList()
            .downcast::<_, CFString>()
            .unwrap()
            .disable_release();
    })
}

#[bench]
fn bench_after(b: &mut Bencher) {
    let string = CFString::from_static_string("Bar");
    b.iter(|| unsafe {
        string.clone()
            .into_CFPropertyList()
            .downcast_into::<_, CFString>()
            .unwrap()
            .disable_release();
    })
}
```
Given the following results:
```
test propertylist::test::bench_after         ... bench:          16 ns/iter (+/- 3)
test propertylist::test::bench_before        ... bench:          73 ns/iter (+/- 14)
```
The final call to `disable_release()` in the benchmarks are to prevent the type coming out of the `unwrap()` from calling `CFRelease` on drop, and thus avoid measuring that. I want to try to measure only `{to,into}_CFPropertyList`, `downcast` and `downcast_into`. The `clone` is needed because the `into` version requires it and it would then not be fair to not have it on both.

I would say that speeding it up by a factor of ~4 can be well worth it? (Well, that 4 is of course no good measurement of anything, microbenchmarks etc. etc.. The benchmark mostly shows that a lot can be gained from avoiding some retain counting.)

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/142)
<!-- Reviewable:end -->
2018-01-20 19:13:21 -06:00
bors-servo 2d35ed2d49 Auto merge of #84 - trlim:filedescriptor, r=jdm
Add bindings for `CFFileDescriptor`

This pull request adds implementation of `CFFileDescriptor` and `CFRunLoopSource` related API.

<!-- Reviewable:start -->

---

This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/84)

<!-- Reviewable:end -->
2018-01-20 17:17:36 -06:00
bors-servo a4cee535b6 Auto merge of #144 - Nemo157:publish-licenses, r=jdm
Include license files in packages

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/144)
<!-- Reviewable:end -->
2018-01-20 13:01:36 -06:00
Linus Färnstrand 3ec0d57863 Convert CFMutableDictionary to use declare_TCFType 2018-01-20 17:15:58 +01:00
Linus Färnstrand 8a52a343c6 Improve documentation and add #[inline] 2018-01-20 16:37:16 +01:00
Linus Färnstrand 3f4f6a0db2 Simplify CFArray test with new downcast method 2018-01-20 16:37:16 +01:00
Linus Färnstrand b13257441d Add cheap casting between CF types 2018-01-20 16:37:16 +01:00
Linus Färnstrand 4fc21d970b Add macro that declare CF types with Drop impl 2018-01-20 16:37:16 +01:00
bors-servo 3a8e10cb4f Auto merge of #135 - djg:dict_mut, r=jdm
Add CFMutableDictionary type.

Added CFMutableDictionary that presents the mutable dictionary
functions of CF. Tries to present a similar interface as CFDictionary.

Fixes issue #93.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/135)
<!-- Reviewable:end -->
2018-01-19 17:25:41 -06:00
Tae-il Lim 9c36257660 Improve tests and code readability 2018-01-18 11:34:04 +09:00
Tae-il Lim 5285fe0836 Change create methods to return Option<T> to handle NULL return value 2018-01-18 11:32:00 +09:00
bors-servo 1bfa6f743b Auto merge of #100 - servo:cargo-workspace, r=jdm
Add Cargo workspace config.

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/100)
<!-- Reviewable:end -->
2018-01-17 19:06:50 -06:00
bors-servo 9d73e041b9 Auto merge of #141 - faern:fix-timing-sensitive-test, r=jdm
Fix timing sensitive test

This problem was found when I tried to debug #132

But since that PR grew so large, and this fix was very unrelated to the work in that PR I split it out to a separate one.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/141)
<!-- Reviewable:end -->
2018-01-17 15:15:24 -06:00
Wim Looman 6bf580ee28 Include license files in packages 2018-01-17 20:52:01 +01:00
Tae-il Lim d6fe0651fe Add filedescriptor test 2018-01-15 17:22:13 +09:00
Tae-il Lim c5d0134e96 Add method to create CFRunLoopSource from CFFileDescriptor 2018-01-15 17:21:37 +09:00
Tae-il Lim 2a19e3f79e Safer handling of CFFileDescriptorContext 2018-01-15 17:20:07 +09:00
Tae-il Lim 530f732ce8 Implement CFFileDescriptor 2018-01-15 14:35:16 +09:00
Linus Färnstrand 3a471a66ba Don't capture output in travis tests 2018-01-14 02:03:33 +01:00
Linus Färnstrand f80f28d3a6 Fix RunLoop test: Increase time tolerance and dont unwind to C 2018-01-14 02:03:33 +01:00
Dan Glastonbury 6a77e377ba Add CFMutableDictionary type.
Added CFMutableDictionary that presents the mutable dictionary
functions of CF. Tries to present a similar interface as CFDictionary.

Fixes issue #93.
2017-11-29 12:50:05 +10:00
bors-servo 665fe0f661 Auto merge of #139 - faern:fix-to-untyped-array, r=jdm
Fix bug in CFArray::to_untyped that made the retain count wrong

The following code segfaults on the `0.4.5` release:
```rust
CFArray::<CFBoolean>::from_CFTypes(&[]).to_untyped().to_untyped().to_untyped()
```
Because `to_untyped` don't increment the retain count, but when the typed array it's created from goes out of scope that one will decrement it.

And since we need to wrap under the get rule (call `CFRetain`) anyway I did not see any reason to have it as a consuming `to_` method. It's more flexible as a borrowing `as_` method.

Since you probably don't want to cause API breakage I kept the old version but fixed the bug and added a deprecation notice on it.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/139)
<!-- Reviewable:end -->
2017-11-28 09:49:12 -06:00
Linus Färnstrand c33598dc99 Bump version to 0.4.6 2017-11-28 16:46:33 +01:00
Linus Färnstrand f0b8535e4e Fix bug in CFType::instance_of. It's not an instance of all other types 2017-11-28 16:46:33 +01:00
Linus Färnstrand 4e9cbd7387 Add test for as_untyped 2017-11-28 16:46:33 +01:00
Linus Färnstrand 31fcb64bf1 Fix bug in CFArray::to_untyped that made the retain count wrong 2017-11-28 16:19:31 +01:00
bors-servo 1a49dbb55c Auto merge of #136 - faern:boolean-from-bool, r=jdm
Add conversion to and from bool for CFBoolean

Make it possible to convert back and fourth between Rust booleans and `CFBoolean`

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/136)
<!-- Reviewable:end -->
2017-11-27 09:38:03 -06:00
Linus Färnstrand f0766c35c2 Add conversion to and from bool for CFBoolean 2017-11-27 16:18:40 +01:00
bors-servo 94d48c20b1 Auto merge of #137 - faern:impl-from-for-cfnumber, r=jdm
Implement From<_> for CFNumber

All the `from_` methods on `CFNumber` can be expressed with implementing the `From` trait and thus enable it to be used a bit more ergonomic in some cases. And feels more idiomatic IMO.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/137)
<!-- Reviewable:end -->
2017-11-27 08:51:43 -06:00
Linus Färnstrand 3ef9e779b0 Implement From<_> for CFNumber 2017-11-27 15:10:40 +01:00
bors-servo 490d24a639 Auto merge of #138 - nox:bump, r=nox
Bump core-foundation-sys version to 0.4.5

This is needed to publish a new core-foundation-rs version.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/138)
<!-- Reviewable:end -->
2017-11-27 07:31:44 -06:00
Anthony Ramine cc66c00ac3 Bump core-foundation-sys version to 0.4.5
This is needed to publish a new core-foundation-rs version.
2017-11-27 14:30:18 +01:00
bors-servo 654a7fdbc1 Auto merge of #133 - jrmuizel:bump-version, r=nox
Bump version for CFArray changes

This is needed for https://github.com/servo/core-graphics-rs/pull/104

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/133)
<!-- Reviewable:end -->
2017-11-27 07:05:50 -06:00
bors-servo 4a9328efcb Auto merge of #134 - jrmuizel:unsafe, r=nox
Mark FromVoid more appropriately as unsafe

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/134)
<!-- Reviewable:end -->
2017-11-27 06:43:42 -06:00
Jeff Muizelaar 8514c48110 Mark FromVoid more appropriately as unsafe 2017-11-26 19:00:44 -05:00
Jeff Muizelaar eb4e452d56 Bump version for CFArray changes 2017-11-26 17:55:51 -05:00
bors-servo f9e854536e Auto merge of #127 - jrmuizel:cfarray-type, r=jdm
Add a type parameter to CFArray

This puts us on a path to greater type safety when using CFArrays

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/127)
<!-- Reviewable:end -->
2017-11-25 14:41:42 -06:00
Jeff Muizelaar 0253ee24ba Add a type parameter to CFArray
This puts us on a path to greater type safety when using CFArrays
2017-11-25 13:02:04 -05:00
bors-servo 063ba16ace Auto merge of #131 - faern:implement-propertylist, r=jdm
Add CFPropertyList and methods for casting to subclasses

https://developer.apple.com/documentation/corefoundation/cfpropertylist-rif specifies the following types as valid subclasess of a property list:
CFData, CFString, CFArray, CFDictionary, CFDate, CFBoolean, and CFNumber.

So I add a trait, `CFPropertyListSubClass`, and implement it for the valid types. Then add the `CFPropertyList` type and make it work as much as possible as the other `TCFType`s. However, it can't implement that trait, since it can't implement the static method `type_id`.

The main point of adding this was to then get to the convenience methods `to_CFPropertyList` and `downcast` that allows safe casting between the subclasses and the superclass. This is handy in calls such as `SCDynamicStoreSetValue` and `SCDynamicStoreCopyValue` where the methods either accept or return a `CFPropertyListRef`. With this addition it becomes easy to write safe wrappers around those functions and make them accept proper, constrained types.

Disclaimer: I'm not yet super familiar with CF. So I can only hope this is safe and sound, but I think it is.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/131)
<!-- Reviewable:end -->
2017-11-24 10:22:57 -06:00
Linus Färnstrand cc1197c371 Change downcast to return Option<T> 2017-11-24 16:30:03 +01:00
Linus Färnstrand 343b0c9a9a Improve documentation with links 2017-11-24 15:48:28 +01:00
Linus Färnstrand 2b7d15941e Add proper error for CFPropertyList::downcast 2017-11-24 15:31:24 +01:00
Linus Färnstrand ac6cc9f5d9 Add CFPropertyList and methods for casting to subclasses 2017-11-24 15:11:29 +01:00
bors-servo 40165e7876 Auto merge of #129 - faern:higher-level-dictionary-find, r=jdm
Higher level dictionary find/contains_key

I'm not very familiar with Core Foundation yet, so tell me if this is not a very helpful addition.

Adding versions of find and contains_key that can take any `TCFType` and automatically converts it to the raw pointer of its corresponding ref type. It looks like dictionaries will mostly contain types implementing this trait, and thus these versions of the methods can make it a bit more ergonomic to use the dictionary. The best would of course be if we could return the correct type as well, but I guess that is not possible with the available information.

Better suggestions for the names are welcome :)

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/129)
<!-- Reviewable:end -->
2017-11-23 08:20:47 -06:00
bors-servo a166cee7d1 Auto merge of #130 - faern:runloop-source-and-observer, r=jdm
Add runloop source and observer functionality

I had the need to add a source (from `SCDynamicStoreCreateRunLoopSource`). And since all the add/contains/remove operations were fairly trivial I did not see any reason to not complete them for timers, sources and observers at the same time.

Also adding the `CFRunLoopMode` type alias to make the signatures reflect the original API a bit more.

Lastly, removing underscore from `_info` since it's actually used.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/130)
<!-- Reviewable:end -->
2017-11-23 08:07:22 -06:00
Linus Färnstrand 7468b455c7 Add runloop source and observer functionality 2017-11-23 13:07:15 +01:00
Linus Färnstrand c445b6e938 Simplify CFDictionary::get with expect() 2017-11-22 20:21:25 +01:00
Linus Färnstrand 64d944780a Simplify and document CFDictionary::get 2017-11-22 16:31:50 +01:00
Linus Färnstrand a076397614 Add higher level methods to CFDictionary 2017-11-22 16:31:50 +01:00