Vendor things

This commit is contained in:
John Doty 2024-03-08 11:03:01 -08:00
parent 5deceec006
commit 977e3c17e5
19434 changed files with 10682014 additions and 0 deletions

View file

@ -0,0 +1,41 @@
use objc2_encode::{Encode, Encoding};
#[cfg(target_pointer_width = "32")]
type CGFloat = f32;
#[cfg(target_pointer_width = "64")]
type CGFloat = f64;
#[repr(C)]
struct CGPoint {
x: CGFloat,
y: CGFloat,
}
unsafe impl Encode for CGPoint {
const ENCODING: Encoding = Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFloat::ENCODING]);
}
#[repr(C)]
struct CGSize {
width: CGFloat,
height: CGFloat,
}
unsafe impl Encode for CGSize {
const ENCODING: Encoding = Encoding::Struct("CGSize", &[CGFloat::ENCODING, CGFloat::ENCODING]);
}
#[repr(C)]
struct CGRect {
origin: CGPoint,
size: CGSize,
}
unsafe impl Encode for CGRect {
const ENCODING: Encoding = Encoding::Struct("CGRect", &[CGPoint::ENCODING, CGSize::ENCODING]);
}
fn main() {
println!("{}", CGRect::ENCODING);
}

View file

@ -0,0 +1,25 @@
use objc2_encode::{Encode, Encoding, RefEncode};
/// We don't know the size of NSString, so we can only hold pointers to it.
///
/// TODO: Use [`extern type`][rfc-1861] when that gets stabilized.
///
/// [rfc-1861]: https://rust-lang.github.io/rfcs/1861-extern-types.html
#[repr(C)]
struct NSString {
_priv: [u8; 0],
}
/// Implement `RefEncode` for pointers and references to the string.
unsafe impl RefEncode for NSString {
const ENCODING_REF: Encoding = Encoding::Object;
}
fn main() {
println!("{}", <*const NSString>::ENCODING);
println!("{}", <*mut NSString>::ENCODING);
println!("{}", <&NSString>::ENCODING);
println!("{}", <&mut NSString>::ENCODING);
println!("{}", Option::<&NSString>::ENCODING);
println!("{}", Option::<&mut NSString>::ENCODING);
}

View file

@ -0,0 +1,32 @@
//! Implementing `Encode` and `RefEncode` for `NSUInteger`.
//!
//! Note that in this case `NSUInteger` could actually just be a type alias
//! for `usize`, and that's already available under `objc2::ffi::NSUInteger`.
use objc2_encode::{Encode, Encoding, RefEncode};
#[repr(transparent)]
struct NSUInteger {
_inner: usize,
}
// SAFETY: `NSUInteger` has the same `repr` as `usize`.
unsafe impl Encode for NSUInteger {
/// Running `@encode(NSUInteger)` gives `Q` on 64-bit systems and `I` on
/// 32-bit systems. This corresponds exactly to `usize`, which is also how
/// we've defined our struct.
const ENCODING: Encoding = usize::ENCODING;
}
// SAFETY: `&NSUInteger` has the same representation as `&usize`.
unsafe impl RefEncode for NSUInteger {
/// Running `@encode(NSUInteger*)` gives `^Q` on 64-bit systems and `^I`
/// on 32-bit systems. So implementing `RefEncode` as a plain pointer is
/// correct.
const ENCODING_REF: Encoding = Encoding::Pointer(&NSUInteger::ENCODING);
}
fn main() {
assert!(NSUInteger::ENCODING.equivalent_to_str("Q"));
assert!(<&NSUInteger>::ENCODING.equivalent_to_str("^Q"));
assert!(<&NSUInteger>::ENCODING.equivalent_to_str("r^Q"));
}

View file

@ -0,0 +1,34 @@
//! Implementing `RefEncode` for `NSDecimal`.
use objc2_encode::{Encoding, RefEncode};
/// We choose in this case to represent `NSDecimal` as an opaque struct
/// (and in the future as an `extern type`) because we don't know much
/// about the internals.
///
/// Therefore we do not implement `Encode`, but when implementing `RefEncode`
/// the type-encoding still has to be correct.
#[repr(C)]
struct NSDecimal {
_priv: [u8; 0],
}
// SAFETY: `&NSDecimal` is a pointer.
unsafe impl RefEncode for NSDecimal {
// Running `@encode` on `NSDecimal*` on my 64-bit system gives `^{?=cCCC[38C]}`.
const ENCODING_REF: Encoding = Encoding::Pointer(&Encoding::Struct(
"?",
&[
Encoding::Char,
Encoding::UChar,
Encoding::UChar,
Encoding::UChar,
Encoding::Array(38, &Encoding::UChar),
],
));
}
fn main() {
assert!(NSDecimal::ENCODING_REF.equivalent_to_str("^{?=cCCC[38C]}"));
// Does not compile:
// println!("{:?}", NSDecimal::ENCODING);
}