@@ -52,11 +52,67 @@ pub struct FFIApplicationData {
5252 pub app_name_length : u32 ,
5353}
5454
55+ // MARK: - ABI Layout Assertions
56+ //
57+ // The four `#[repr(C)]` structs above are passed by value (or via packed
58+ // buffers) across the Rust <-> Swift `@_cdecl` FFI boundary. Their Swift
59+ // counterparts live in `swift-bridge/Sources/ScreenCaptureKitBridge/Core.swift`
60+ // (`@frozen public struct FFIRect/FFIDisplayData/FFIWindowData/FFIApplicationData`).
61+ //
62+ // These compile-time assertions pin the exact ABI shared with Swift: any change
63+ // to a field type, field order, or padding fails the build immediately instead
64+ // of silently corrupting marshalled data at runtime. If you change the layout
65+ // here you MUST mirror it in Core.swift (and vice versa); the cross-language
66+ // `sc_verify_ffi_layout` check in `tests/ffi_layout_tests.rs` guards that too.
67+ use core:: mem:: { align_of, offset_of, size_of} ;
68+
69+ const _: ( ) = assert ! ( size_of:: <FFIRect >( ) == 32 ) ;
70+ const _: ( ) = assert ! ( align_of:: <FFIRect >( ) == 8 ) ;
71+ const _: ( ) = assert ! ( offset_of!( FFIRect , x) == 0 ) ;
72+ const _: ( ) = assert ! ( offset_of!( FFIRect , y) == 8 ) ;
73+ const _: ( ) = assert ! ( offset_of!( FFIRect , width) == 16 ) ;
74+ const _: ( ) = assert ! ( offset_of!( FFIRect , height) == 24 ) ;
75+
76+ const _: ( ) = assert ! ( size_of:: <FFIDisplayData >( ) == 48 ) ;
77+ const _: ( ) = assert ! ( align_of:: <FFIDisplayData >( ) == 8 ) ;
78+ const _: ( ) = assert ! ( offset_of!( FFIDisplayData , display_id) == 0 ) ;
79+ const _: ( ) = assert ! ( offset_of!( FFIDisplayData , width) == 4 ) ;
80+ const _: ( ) = assert ! ( offset_of!( FFIDisplayData , height) == 8 ) ;
81+ const _: ( ) = assert ! ( offset_of!( FFIDisplayData , frame) == 16 ) ;
82+
83+ const _: ( ) = assert ! ( size_of:: <FFIWindowData >( ) == 64 ) ;
84+ const _: ( ) = assert ! ( align_of:: <FFIWindowData >( ) == 8 ) ;
85+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , window_id) == 0 ) ;
86+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , window_layer) == 4 ) ;
87+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , is_on_screen) == 8 ) ;
88+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , is_active) == 9 ) ;
89+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , frame) == 16 ) ;
90+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , title_offset) == 48 ) ;
91+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , title_length) == 52 ) ;
92+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , owning_app_index) == 56 ) ;
93+ const _: ( ) = assert ! ( offset_of!( FFIWindowData , _padding) == 60 ) ;
94+
95+ const _: ( ) = assert ! ( size_of:: <FFIApplicationData >( ) == 24 ) ;
96+ const _: ( ) = assert ! ( align_of:: <FFIApplicationData >( ) == 4 ) ;
97+ const _: ( ) = assert ! ( offset_of!( FFIApplicationData , process_id) == 0 ) ;
98+ const _: ( ) = assert ! ( offset_of!( FFIApplicationData , _padding) == 4 ) ;
99+ const _: ( ) = assert ! ( offset_of!( FFIApplicationData , bundle_id_offset) == 8 ) ;
100+ const _: ( ) = assert ! ( offset_of!( FFIApplicationData , bundle_id_length) == 12 ) ;
101+ const _: ( ) = assert ! ( offset_of!( FFIApplicationData , app_name_offset) == 16 ) ;
102+ const _: ( ) = assert ! ( offset_of!( FFIApplicationData , app_name_length) == 20 ) ;
103+
55104// MARK: - CoreGraphics Initialization
56105extern "C" {
57106 /// Force CoreGraphics initialization by calling `CGMainDisplayID`
58107 /// This prevents `CGS_REQUIRE_INIT` crashes on headless systems
59108 pub fn sc_initialize_core_graphics ( ) ;
109+
110+ /// Cross-language ABI check implemented in the Swift bridge.
111+ ///
112+ /// Returns `true` only if the Swift `MemoryLayout` (size, stride and
113+ /// alignment) of all four FFI structs matches the values pinned on the
114+ /// Rust side. Verified by `tests/ffi_layout_tests.rs`.
115+ pub fn sc_verify_ffi_layout ( ) -> bool ;
60116}
61117
62118// MARK: - SCShareableContent
0 commit comments