Skip to content

Commit 68e3a7f

Browse files
committed
Add NSString example
Important to showcase how to implement Encode for Objective-C objects that we don't know the internal representation of.
1 parent 43534c8 commit 68e3a7f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

examples/ns_string.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use objc_encode::{Encode, Encoding};
2+
3+
/// We don't know the size of NSString, so we can only hold pointers to it.
4+
///
5+
/// TODO: Use [`extern type`][rfc-1861] when that gets stabilized.
6+
///
7+
/// [rfc-1861]: https://rust-lang.github.io/rfcs/1861-extern-types.html
8+
#[repr(C)]
9+
struct NSString {
10+
_priv: [u8; 0],
11+
}
12+
13+
/// Implement `Encode` for references.
14+
///
15+
/// This also implements for `*mut NSString` and `Option<&mut NSString>`.
16+
unsafe impl<'a> Encode for &'a NSString {
17+
const ENCODING: Encoding<'static> = Encoding::Object;
18+
}
19+
20+
/// Implement `Encode` for mutable references.
21+
///
22+
/// This also implements for `*mut NSString` and `Option<&mut NSString>`.
23+
unsafe impl<'a> Encode for &'a mut NSString {
24+
const ENCODING: Encoding<'static> = Encoding::Object;
25+
}

0 commit comments

Comments
 (0)