Skip to content

Commit

Permalink
Feature-gate block support in objc_foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 31, 2021
1 parent e62ea58 commit e0deb58
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --no-fail-fast
# TODO: `objc/exception` feature is broken in objc_foundation
# TODO: `objc_foundation/block` feature doesn't work on GNUStep
args: --verbose --no-fail-fast --no-default-features
5 changes: 4 additions & 1 deletion objc_foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ license = "MIT"

exclude = [".gitignore"]

[features]
default = ["block"]

[dependencies]
block = "0.1"
block = { optional = true, version = "0.1" }
objc = { path = "../objc", version = "0.2.7" }
objc_id = { path = "../objc_id", version = "0.1" }
6 changes: 4 additions & 2 deletions objc_foundation/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::mem;
use std::ops::Range;
use std::os::raw::c_void;
use std::slice;

use super::{INSCopying, INSMutableCopying, INSObject, NSRange};
#[cfg(feature = "block")]
use block::{Block, ConcreteBlock};
use objc::msg_send;
use objc_id::Id;
Expand Down Expand Up @@ -39,6 +39,7 @@ pub trait INSData: INSObject {
}
}

#[cfg(feature = "block")]
fn from_vec(bytes: Vec<u8>) -> Id<Self> {
let capacity = bytes.capacity();
let dealloc = ConcreteBlock::new(move |bytes: *mut c_void, len: usize| unsafe {
Expand All @@ -56,7 +57,7 @@ pub trait INSData: INSObject {
let obj: *mut Self = msg_send![obj, initWithBytesNoCopy:bytes_ptr
length:bytes.len()
deallocator:dealloc];
mem::forget(bytes);
std::mem::forget(bytes);
Id::from_retained_ptr(obj)
}
}
Expand Down Expand Up @@ -192,6 +193,7 @@ mod tests {
assert!(data.bytes() == [8, 17]);
}

#[cfg(feature = "block")]
#[test]
fn test_from_vec() {
let bytes = vec![3, 7, 16];
Expand Down

0 comments on commit e0deb58

Please sign in to comment.