boxdd 0.4.0

Safe, ergonomic Rust bindings for Box2D v3
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
use std::marker::PhantomData;

use crate::body::{Body, OwnedBody};
use crate::error::{ApiError, ApiResult};
use crate::shapes::SurfaceMaterial;
use crate::types::{BodyId, ChainId, ShapeId, Vec2};
use crate::world::World;
use boxdd_sys::ffi;
use std::rc::Rc;
use std::sync::Arc;

const _: () = {
    assert!(core::mem::size_of::<Vec2>() == core::mem::size_of::<ffi::b2Vec2>());
    assert!(core::mem::align_of::<Vec2>() == core::mem::align_of::<ffi::b2Vec2>());
    assert!(
        core::mem::size_of::<SurfaceMaterial>() == core::mem::size_of::<ffi::b2SurfaceMaterial>()
    );
    assert!(
        core::mem::align_of::<SurfaceMaterial>() == core::mem::align_of::<ffi::b2SurfaceMaterial>()
    );
};

/// How a `ChainDef` provides surface materials to Box2D.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ChainDefMaterialLayout<'a> {
    /// Use Box2D's default chain material.
    Default(SurfaceMaterial),
    /// Use one material for the entire chain.
    Single(SurfaceMaterial),
    /// Use one material entry for every stored chain point.
    Multiple(&'a [SurfaceMaterial]),
}

impl<'a> ChainDefMaterialLayout<'a> {
    /// Number of material entries visible to Box2D.
    pub const fn count(&self) -> usize {
        match self {
            Self::Default(_) | Self::Single(_) => 1,
            Self::Multiple(materials) => materials.len(),
        }
    }
}

/// A scoped chain handle tied to a mutable borrow of the world.
pub struct Chain<'w> {
    pub(crate) id: ChainId,
    #[allow(dead_code)]
    pub(crate) core: Arc<crate::core::world_core::WorldCore>,
    _world: PhantomData<&'w World>,
}

/// A RAII-owned chain that is destroyed on drop.
pub struct OwnedChain {
    id: ChainId,
    core: Arc<crate::core::world_core::WorldCore>,
    destroy_on_drop: bool,
    _not_send: PhantomData<Rc<()>>,
}

#[inline]
fn raw_chain_id(id: ChainId) -> ffi::b2ChainId {
    id.into_raw()
}

fn chain_segments_into_impl(id: ChainId, out: &mut Vec<ShapeId>) {
    let id = raw_chain_id(id);
    let count = unsafe { ffi::b2Chain_GetSegmentCount(id) }.max(0) as usize;
    unsafe {
        crate::core::ffi_vec::fill_from_ffi(out, count, |ptr, count| {
            ffi::b2Chain_GetSegments(id, ptr.cast(), count)
        });
    }
}

fn chain_segments_impl(id: ChainId) -> Vec<ShapeId> {
    let id = raw_chain_id(id);
    let count = unsafe { ffi::b2Chain_GetSegmentCount(id) }.max(0) as usize;
    unsafe {
        crate::core::ffi_vec::read_from_ffi(count, |ptr: *mut ShapeId, count| {
            ffi::b2Chain_GetSegments(id, ptr.cast(), count)
        })
    }
}

fn chain_segment_count_checked_impl(id: ChainId) -> i32 {
    crate::core::debug_checks::assert_chain_valid(id);
    chain_segment_count_impl(id)
}

fn try_chain_segment_count_impl(id: ChainId) -> ApiResult<i32> {
    crate::core::debug_checks::check_chain_valid(id)?;
    Ok(chain_segment_count_impl(id))
}

fn chain_segments_checked_impl(id: ChainId) -> Vec<ShapeId> {
    crate::core::debug_checks::assert_chain_valid(id);
    chain_segments_impl(id)
}

fn chain_segments_into_checked_impl(id: ChainId, out: &mut Vec<ShapeId>) {
    crate::core::debug_checks::assert_chain_valid(id);
    chain_segments_into_impl(id, out);
}

fn try_chain_segments_impl(id: ChainId) -> ApiResult<Vec<ShapeId>> {
    crate::core::debug_checks::check_chain_valid(id)?;
    Ok(chain_segments_impl(id))
}

fn try_chain_segments_into_impl(id: ChainId, out: &mut Vec<ShapeId>) -> ApiResult<()> {
    crate::core::debug_checks::check_chain_valid(id)?;
    chain_segments_into_impl(id, out);
    Ok(())
}

#[inline]
fn chain_world_id_impl(id: ChainId) -> ffi::b2WorldId {
    unsafe { ffi::b2Chain_GetWorld(raw_chain_id(id)) }
}

#[inline]
fn chain_world_id_checked_impl(id: ChainId) -> ffi::b2WorldId {
    crate::core::debug_checks::assert_chain_valid(id);
    chain_world_id_impl(id)
}

#[inline]
fn try_chain_world_id_raw_impl(id: ChainId) -> ApiResult<ffi::b2WorldId> {
    crate::core::debug_checks::check_chain_valid(id)?;
    Ok(chain_world_id_impl(id))
}

#[inline]
fn chain_is_valid_impl(id: ChainId) -> bool {
    unsafe { ffi::b2Chain_IsValid(raw_chain_id(id)) }
}

#[inline]
fn chain_is_valid_checked_impl(id: ChainId) -> bool {
    crate::core::callback_state::assert_not_in_callback();
    chain_is_valid_impl(id)
}

#[inline]
fn try_chain_is_valid_impl(id: ChainId) -> ApiResult<bool> {
    crate::core::callback_state::check_not_in_callback()?;
    Ok(chain_is_valid_impl(id))
}

#[inline]
fn chain_segment_count_impl(id: ChainId) -> i32 {
    unsafe { ffi::b2Chain_GetSegmentCount(raw_chain_id(id)) }
}

#[inline]
fn chain_raw_surface_material_count_impl(id: ChainId) -> i32 {
    unsafe { ffi::b2Chain_GetSurfaceMaterialCount(raw_chain_id(id)) }
}

#[inline]
fn chain_runtime_surface_material_layout_impl(id: ChainId) -> (i32, i32) {
    let raw_count = chain_raw_surface_material_count_impl(id);
    let segment_count = chain_segment_count_impl(id);
    debug_assert!(
        raw_count == 1 || raw_count == segment_count || raw_count == segment_count + 3,
        "unexpected chain material layout: raw_count={raw_count}, segment_count={segment_count}"
    );
    (raw_count, segment_count)
}

#[inline]
fn chain_surface_material_count_impl(id: ChainId) -> i32 {
    let (raw_count, segment_count) = chain_runtime_surface_material_layout_impl(id);
    if raw_count == 1 { 1 } else { segment_count }
}

#[inline]
fn chain_set_surface_material_impl(id: ChainId, index: i32, material: &SurfaceMaterial) {
    let (raw_count, segment_count) = chain_runtime_surface_material_layout_impl(id);
    if raw_count == 1 {
        unsafe { ffi::b2Chain_SetSurfaceMaterial(raw_chain_id(id), &material.0, 0) }
    } else if raw_count == segment_count {
        unsafe { ffi::b2Chain_SetSurfaceMaterial(raw_chain_id(id), &material.0, index) }
    } else {
        let segment = chain_segments_impl(id)[index as usize];
        crate::shapes::shape_set_surface_material_impl(segment, material);
    }
}

#[inline]
fn chain_surface_material_impl(id: ChainId, index: i32) -> SurfaceMaterial {
    let (raw_count, segment_count) = chain_runtime_surface_material_layout_impl(id);
    if raw_count == 1 {
        SurfaceMaterial::from_raw(unsafe { ffi::b2Chain_GetSurfaceMaterial(raw_chain_id(id), 0) })
    } else if raw_count == segment_count {
        SurfaceMaterial::from_raw(unsafe {
            ffi::b2Chain_GetSurfaceMaterial(raw_chain_id(id), index)
        })
    } else {
        let segment = chain_segments_impl(id)[index as usize];
        crate::shapes::shape_surface_material_impl(segment)
    }
}

#[track_caller]
fn assert_chain_surface_material_index_in_range(id: ChainId, index: i32) {
    let count = chain_surface_material_count_impl(id);
    assert!(
        0 <= index && index < count,
        "chain surface material index out of range: index={index}, visible_count={count}"
    );
}

fn check_chain_surface_material_index_in_range(id: ChainId, index: i32) -> ApiResult<()> {
    let count = chain_surface_material_count_impl(id);
    if 0 <= index && index < count {
        Ok(())
    } else {
        Err(ApiError::IndexOutOfRange)
    }
}

fn chain_surface_material_count_checked_impl(id: ChainId) -> i32 {
    crate::core::debug_checks::assert_chain_valid(id);
    chain_surface_material_count_impl(id)
}

fn try_chain_surface_material_count_impl(id: ChainId) -> ApiResult<i32> {
    crate::core::debug_checks::check_chain_valid(id)?;
    Ok(chain_surface_material_count_impl(id))
}

fn chain_set_surface_material_checked_impl(id: ChainId, index: i32, material: &SurfaceMaterial) {
    crate::core::debug_checks::assert_chain_valid(id);
    assert_chain_surface_material_index_in_range(id, index);
    chain_set_surface_material_impl(id, index, material)
}

fn try_chain_set_surface_material_impl(
    id: ChainId,
    index: i32,
    material: &SurfaceMaterial,
) -> ApiResult<()> {
    crate::core::debug_checks::check_chain_valid(id)?;
    check_chain_surface_material_index_in_range(id, index)?;
    chain_set_surface_material_impl(id, index, material);
    Ok(())
}

fn chain_surface_material_checked_impl(id: ChainId, index: i32) -> SurfaceMaterial {
    crate::core::debug_checks::assert_chain_valid(id);
    assert_chain_surface_material_index_in_range(id, index);
    chain_surface_material_impl(id, index)
}

fn try_chain_surface_material_impl(id: ChainId, index: i32) -> ApiResult<SurfaceMaterial> {
    crate::core::debug_checks::check_chain_valid(id)?;
    check_chain_surface_material_index_in_range(id, index)?;
    Ok(chain_surface_material_impl(id, index))
}

#[inline]
fn destroy_chain_now_impl(world_core: &crate::core::world_core::WorldCore, id: ChainId) {
    unsafe { ffi::b2DestroyChain(raw_chain_id(id)) }
    #[cfg(feature = "serialize")]
    world_core.remove_chain(id);
    #[cfg(not(feature = "serialize"))]
    let _ = world_core;
}

fn destroy_owned_chain_if_needed_impl(
    world_core: &crate::core::world_core::WorldCore,
    id: ChainId,
) {
    if !chain_is_valid_impl(id) {
        return;
    }

    if crate::core::callback_state::in_callback() || world_core.events_buffers_are_borrowed() {
        world_core.defer_destroy(crate::core::world_core::DeferredDestroy::Chain(id));
    } else {
        destroy_chain_now_impl(world_core, id);
    }
}

fn destroy_scoped_chain_checked_impl(world_core: &crate::core::world_core::WorldCore, id: ChainId) {
    crate::core::callback_state::assert_not_in_callback();
    if chain_is_valid_impl(id) {
        destroy_chain_now_impl(world_core, id);
    }
}

fn try_destroy_scoped_chain_impl(
    world_core: &crate::core::world_core::WorldCore,
    id: ChainId,
) -> ApiResult<()> {
    crate::core::debug_checks::check_chain_valid(id)?;
    if chain_is_valid_impl(id) {
        destroy_chain_now_impl(world_core, id);
    }
    Ok(())
}

trait ChainRuntimeHandle {
    fn chain_id(&self) -> ChainId;

    fn handle_world_id_raw(&self) -> ffi::b2WorldId {
        chain_world_id_checked_impl(self.chain_id())
    }

    fn try_handle_world_id_raw(&self) -> ApiResult<ffi::b2WorldId> {
        try_chain_world_id_raw_impl(self.chain_id())
    }

    fn handle_is_valid(&self) -> bool {
        chain_is_valid_checked_impl(self.chain_id())
    }

    fn try_handle_is_valid(&self) -> ApiResult<bool> {
        try_chain_is_valid_impl(self.chain_id())
    }

    fn handle_segment_count(&self) -> i32 {
        chain_segment_count_checked_impl(self.chain_id())
    }

    fn try_handle_segment_count(&self) -> ApiResult<i32> {
        try_chain_segment_count_impl(self.chain_id())
    }

    fn handle_segments(&self) -> Vec<ShapeId> {
        chain_segments_checked_impl(self.chain_id())
    }

    fn handle_segments_into(&self, out: &mut Vec<ShapeId>) {
        chain_segments_into_checked_impl(self.chain_id(), out);
    }

    fn try_handle_segments(&self) -> ApiResult<Vec<ShapeId>> {
        try_chain_segments_impl(self.chain_id())
    }

    fn try_handle_segments_into(&self, out: &mut Vec<ShapeId>) -> ApiResult<()> {
        try_chain_segments_into_impl(self.chain_id(), out)
    }

    fn handle_surface_material_count(&self) -> i32 {
        chain_surface_material_count_checked_impl(self.chain_id())
    }

    fn try_handle_surface_material_count(&self) -> ApiResult<i32> {
        try_chain_surface_material_count_impl(self.chain_id())
    }

    fn handle_set_surface_material(&mut self, index: i32, material: &SurfaceMaterial) {
        chain_set_surface_material_checked_impl(self.chain_id(), index, material)
    }

    fn try_handle_set_surface_material(
        &mut self,
        index: i32,
        material: &SurfaceMaterial,
    ) -> ApiResult<()> {
        try_chain_set_surface_material_impl(self.chain_id(), index, material)
    }

    fn handle_surface_material(&self, index: i32) -> SurfaceMaterial {
        chain_surface_material_checked_impl(self.chain_id(), index)
    }

    fn try_handle_surface_material(&self, index: i32) -> ApiResult<SurfaceMaterial> {
        try_chain_surface_material_impl(self.chain_id(), index)
    }
}

impl ChainRuntimeHandle for OwnedChain {
    fn chain_id(&self) -> ChainId {
        self.id
    }
}

impl<'w> ChainRuntimeHandle for Chain<'w> {
    fn chain_id(&self) -> ChainId {
        self.id
    }
}

impl OwnedChain {
    pub(crate) fn new(core: Arc<crate::core::world_core::WorldCore>, id: ChainId) -> Self {
        core.owned_chains
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        Self {
            id,
            core,
            destroy_on_drop: true,
            _not_send: PhantomData,
        }
    }

    pub fn id(&self) -> ChainId {
        self.id
    }

    pub fn world_id_raw(&self) -> ffi::b2WorldId {
        ChainRuntimeHandle::handle_world_id_raw(self)
    }

    pub fn try_world_id_raw(&self) -> ApiResult<ffi::b2WorldId> {
        ChainRuntimeHandle::try_handle_world_id_raw(self)
    }

    pub fn is_valid(&self) -> bool {
        ChainRuntimeHandle::handle_is_valid(self)
    }

    pub fn try_is_valid(&self) -> ApiResult<bool> {
        ChainRuntimeHandle::try_handle_is_valid(self)
    }

    /// Borrow the raw id for ID-style APIs.
    pub fn as_id(&self) -> ChainId {
        self.id
    }

    pub fn segment_count(&self) -> i32 {
        ChainRuntimeHandle::handle_segment_count(self)
    }

    pub fn try_segment_count(&self) -> ApiResult<i32> {
        ChainRuntimeHandle::try_handle_segment_count(self)
    }

    /// Collect all segment shape ids for this chain.
    pub fn segments(&self) -> Vec<ShapeId> {
        ChainRuntimeHandle::handle_segments(self)
    }

    pub fn segments_into(&self, out: &mut Vec<ShapeId>) {
        ChainRuntimeHandle::handle_segments_into(self, out);
    }

    pub fn try_segments(&self) -> ApiResult<Vec<ShapeId>> {
        ChainRuntimeHandle::try_handle_segments(self)
    }

    pub fn try_segments_into(&self, out: &mut Vec<ShapeId>) -> ApiResult<()> {
        ChainRuntimeHandle::try_handle_segments_into(self, out)
    }

    /// Number of runtime-visible material slots on this chain.
    ///
    /// Open chains normalize Box2D's ghost-point placeholder layout down to the number of
    /// live segments. Single-material chains still report `1`.
    pub fn surface_material_count(&self) -> i32 {
        ChainRuntimeHandle::handle_surface_material_count(self)
    }
    pub fn try_surface_material_count(&self) -> ApiResult<i32> {
        ChainRuntimeHandle::try_handle_surface_material_count(self)
    }
    /// Set a runtime-visible material slot by segment index.
    pub fn set_surface_material(&mut self, index: i32, material: &SurfaceMaterial) {
        ChainRuntimeHandle::handle_set_surface_material(self, index, material)
    }
    pub fn try_set_surface_material(
        &mut self,
        index: i32,
        material: &SurfaceMaterial,
    ) -> ApiResult<()> {
        ChainRuntimeHandle::try_handle_set_surface_material(self, index, material)
    }
    /// Read a runtime-visible material slot by segment index.
    pub fn surface_material(&self, index: i32) -> SurfaceMaterial {
        ChainRuntimeHandle::handle_surface_material(self, index)
    }

    pub fn try_surface_material(&self, index: i32) -> ApiResult<SurfaceMaterial> {
        ChainRuntimeHandle::try_handle_surface_material(self, index)
    }

    pub fn into_id(mut self) -> ChainId {
        self.destroy_on_drop = false;
        self.id
    }

    pub fn destroy(mut self) {
        if self.destroy_on_drop {
            destroy_owned_chain_if_needed_impl(&self.core, self.id);
        }
        self.destroy_on_drop = false;
    }
}

impl Drop for OwnedChain {
    fn drop(&mut self) {
        let _ = self.core.id;
        let prev = self
            .core
            .owned_chains
            .fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
        debug_assert!(prev > 0, "owned chain counter underflow");
        if self.destroy_on_drop {
            destroy_owned_chain_if_needed_impl(&self.core, self.id);
        }
    }
}

impl<'w> Chain<'w> {
    pub(crate) fn new(core: Arc<crate::core::world_core::WorldCore>, id: ChainId) -> Self {
        Self {
            id,
            core,
            _world: PhantomData,
        }
    }

    pub fn id(&self) -> ChainId {
        self.id
    }

    pub fn world_id_raw(&self) -> ffi::b2WorldId {
        ChainRuntimeHandle::handle_world_id_raw(self)
    }

    pub fn try_world_id_raw(&self) -> ApiResult<ffi::b2WorldId> {
        ChainRuntimeHandle::try_handle_world_id_raw(self)
    }

    pub fn is_valid(&self) -> bool {
        ChainRuntimeHandle::handle_is_valid(self)
    }

    pub fn try_is_valid(&self) -> ApiResult<bool> {
        ChainRuntimeHandle::try_handle_is_valid(self)
    }

    pub fn segment_count(&self) -> i32 {
        ChainRuntimeHandle::handle_segment_count(self)
    }

    pub fn try_segment_count(&self) -> ApiResult<i32> {
        ChainRuntimeHandle::try_handle_segment_count(self)
    }

    /// Collect all segment shape ids for this chain.
    pub fn segments(&self) -> Vec<ShapeId> {
        ChainRuntimeHandle::handle_segments(self)
    }

    pub fn segments_into(&self, out: &mut Vec<ShapeId>) {
        ChainRuntimeHandle::handle_segments_into(self, out);
    }

    pub fn try_segments(&self) -> ApiResult<Vec<ShapeId>> {
        ChainRuntimeHandle::try_handle_segments(self)
    }

    pub fn try_segments_into(&self, out: &mut Vec<ShapeId>) -> ApiResult<()> {
        ChainRuntimeHandle::try_handle_segments_into(self, out)
    }

    /// Number of runtime-visible material slots on this chain.
    ///
    /// Open chains normalize Box2D's ghost-point placeholder layout down to the number of
    /// live segments. Single-material chains still report `1`.
    pub fn surface_material_count(&self) -> i32 {
        ChainRuntimeHandle::handle_surface_material_count(self)
    }
    pub fn try_surface_material_count(&self) -> ApiResult<i32> {
        ChainRuntimeHandle::try_handle_surface_material_count(self)
    }

    /// Set a runtime-visible material slot by segment index.
    pub fn set_surface_material(&mut self, index: i32, material: &SurfaceMaterial) {
        ChainRuntimeHandle::handle_set_surface_material(self, index, material)
    }

    pub fn try_set_surface_material(
        &mut self,
        index: i32,
        material: &SurfaceMaterial,
    ) -> ApiResult<()> {
        ChainRuntimeHandle::try_handle_set_surface_material(self, index, material)
    }

    /// Read a runtime-visible material slot by segment index.
    pub fn surface_material(&self, index: i32) -> SurfaceMaterial {
        ChainRuntimeHandle::handle_surface_material(self, index)
    }

    pub fn try_surface_material(&self, index: i32) -> ApiResult<SurfaceMaterial> {
        ChainRuntimeHandle::try_handle_surface_material(self, index)
    }

    /// Destroy this chain immediately.
    pub fn destroy(self) {
        destroy_scoped_chain_checked_impl(&self.core, self.id);
    }

    pub fn try_destroy(self) -> ApiResult<()> {
        try_destroy_scoped_chain_impl(&self.core, self.id)
    }
}

/// Chain shape definition. Holds optional owned data for points and materials.
#[derive(Debug)]
pub struct ChainDef {
    pub(crate) def: ffi::b2ChainDef,
    points: Vec<ffi::b2Vec2>,
    materials: Vec<ffi::b2SurfaceMaterial>,
}

impl Clone for ChainDef {
    fn clone(&self) -> Self {
        let mut def = self.def;
        let points = self.points.clone();
        let materials = self.materials.clone();

        if points.is_empty() {
            def.points = core::ptr::null();
            def.count = 0;
        } else {
            def.points = points.as_ptr();
            def.count = points.len() as i32;
        }

        if materials.is_empty() {
            // Keep default material pointer/count stable.
            let default_def = unsafe { ffi::b2DefaultChainDef() };
            def.materials = default_def.materials;
            def.materialCount = default_def.materialCount;
        } else {
            def.materials = materials.as_ptr();
            def.materialCount = materials.len() as i32;
        }

        Self {
            def,
            points,
            materials,
        }
    }
}

impl Default for ChainDef {
    fn default() -> Self {
        Self {
            def: unsafe { ffi::b2DefaultChainDef() },
            points: Vec::new(),
            materials: Vec::new(),
        }
    }
}

impl ChainDef {
    /// Start building a new `ChainDef` from defaults.
    pub fn builder() -> ChainDefBuilder {
        ChainDefBuilder {
            inner: Self::default(),
        }
    }

    /// Stored chain points, including Box2D's ghost points.
    pub fn points(&self) -> &[Vec2] {
        unsafe {
            core::slice::from_raw_parts(self.points.as_ptr().cast::<Vec2>(), self.points.len())
        }
    }

    /// Whether the chain is closed into a loop.
    pub const fn is_loop(&self) -> bool {
        self.def.isLoop
    }

    /// Collision filter used by the chain.
    pub const fn filter(&self) -> crate::filter::Filter {
        crate::filter::Filter::from_raw(self.def.filter)
    }

    /// Whether sensor begin/end events are enabled for the chain.
    pub const fn sensor_events_enabled(&self) -> bool {
        self.def.enableSensorEvents
    }

    /// Inspect the material layout supplied to the chain definition.
    pub fn material_layout(&self) -> ChainDefMaterialLayout<'_> {
        match self.materials.len() {
            0 => ChainDefMaterialLayout::Default(SurfaceMaterial::from_raw(unsafe {
                *self.def.materials
            })),
            1 => ChainDefMaterialLayout::Single(SurfaceMaterial::from_raw(self.materials[0])),
            _ => ChainDefMaterialLayout::Multiple(unsafe {
                core::slice::from_raw_parts(
                    self.materials.as_ptr().cast::<SurfaceMaterial>(),
                    self.materials.len(),
                )
            }),
        }
    }

    /// Number of material entries visible to Box2D.
    pub fn material_count(&self) -> usize {
        self.material_layout().count()
    }

    #[cfg(feature = "serialize")]
    pub(crate) fn points_raw_slice(&self) -> &[ffi::b2Vec2] {
        &self.points
    }
    #[cfg(feature = "serialize")]
    pub(crate) fn materials_raw_slice(&self) -> &[ffi::b2SurfaceMaterial] {
        &self.materials
    }
}

#[derive(Clone, Debug)]
pub struct ChainDefBuilder {
    inner: ChainDef,
}

impl ChainDefBuilder {
    pub fn points<I, P>(mut self, points: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<crate::types::Vec2>,
    {
        self.inner.points = points.into_iter().map(|p| p.into().into_raw()).collect();
        self.inner.def.points = if self.inner.points.is_empty() {
            core::ptr::null()
        } else {
            self.inner.points.as_ptr()
        };
        self.inner.def.count = self.inner.points.len() as i32;
        self
    }
    pub fn is_loop(mut self, v: bool) -> Self {
        self.inner.def.isLoop = v;
        self
    }
    pub fn filter(mut self, f: crate::filter::Filter) -> Self {
        self.inner.def.filter = f.into_raw();
        self
    }
    pub fn filter_raw(mut self, f: ffi::b2Filter) -> Self {
        self.inner.def.filter = f;
        self
    }
    pub fn enable_sensor_events(mut self, v: bool) -> Self {
        self.inner.def.enableSensorEvents = v;
        self
    }
    pub fn single_material(mut self, m: &SurfaceMaterial) -> Self {
        self.inner.materials.clear();
        self.inner.materials.push(m.0);
        self.inner.def.materials = self.inner.materials.as_ptr();
        self.inner.def.materialCount = 1;
        self
    }
    pub fn materials(mut self, mats: &[SurfaceMaterial]) -> Self {
        if mats.is_empty() {
            self.inner.materials.clear();
            // Reset to the upstream default material (static storage on the C side).
            let default_def = unsafe { ffi::b2DefaultChainDef() };
            self.inner.def.materials = default_def.materials;
            self.inner.def.materialCount = default_def.materialCount;
        } else {
            self.inner.materials = mats.iter().map(|m| m.0).collect();
            self.inner.def.materials = self.inner.materials.as_ptr();
            self.inner.def.materialCount = self.inner.materials.len() as i32;
        }
        self
    }
    #[must_use]
    pub fn build(mut self) -> ChainDef {
        if self.inner.def.count == 0 {
            // ensure sane default
            self.inner.points.clear();
            self.inner.def.points = core::ptr::null();
        }
        self.inner
    }
}

impl From<ChainDef> for ChainDefBuilder {
    fn from(def: ChainDef) -> Self {
        Self { inner: def }
    }
}

#[inline]
#[track_caller]
pub(crate) fn assert_chain_def_valid(def: &ChainDef) {
    let count = def.def.count;
    assert!(
        count >= 4,
        "invalid ChainDef: expected at least 4 points (including ghosts), got {count}"
    );
    assert!(
        !def.def.points.is_null(),
        "invalid ChainDef: points pointer is null"
    );
    let mc = def.def.materialCount;
    assert!(
        mc == 1 || mc == count,
        "invalid ChainDef: materialCount must be 1 or equal to count (materialCount={mc}, count={count})"
    );
    assert!(
        !def.def.materials.is_null(),
        "invalid ChainDef: materials pointer is null"
    );
}

pub(crate) fn check_chain_def_valid(def: &ChainDef) -> ApiResult<()> {
    let count = def.def.count;
    if count < 4 {
        return Err(ApiError::InvalidChainDef);
    }
    if def.def.points.is_null() {
        return Err(ApiError::InvalidChainDef);
    }
    let mc = def.def.materialCount;
    if mc != 1 && mc != count {
        return Err(ApiError::InvalidChainDef);
    }
    if def.def.materials.is_null() {
        return Err(ApiError::InvalidChainDef);
    }
    Ok(())
}

pub(crate) fn create_chain_for_body_impl(
    core: &crate::core::world_core::WorldCore,
    body: BodyId,
    def: &ChainDef,
) -> ChainId {
    crate::core::debug_checks::assert_body_valid(body);
    assert_chain_def_valid(def);
    let id = ChainId::from_raw(unsafe { ffi::b2CreateChain(body.into_raw(), &def.def) });
    #[cfg(feature = "serialize")]
    {
        let meta = crate::core::serialize_registry::ChainCreateMeta::from_def(body, def);
        core.record_chain(id, meta);
    }
    #[cfg(not(feature = "serialize"))]
    let _ = core;
    id
}

pub(crate) fn try_create_chain_for_body_impl(
    core: &crate::core::world_core::WorldCore,
    body: BodyId,
    def: &ChainDef,
) -> ApiResult<ChainId> {
    crate::core::debug_checks::check_body_valid(body)?;
    check_chain_def_valid(def)?;
    let id = ChainId::from_raw(unsafe { ffi::b2CreateChain(body.into_raw(), &def.def) });
    #[cfg(feature = "serialize")]
    {
        let meta = crate::core::serialize_registry::ChainCreateMeta::from_def(body, def);
        core.record_chain(id, meta);
    }
    #[cfg(not(feature = "serialize"))]
    let _ = core;
    Ok(id)
}

fn create_body_attached_chain_handle<T>(
    core: &Arc<crate::core::world_core::WorldCore>,
    body: BodyId,
    def: &ChainDef,
    create: impl FnOnce(&crate::core::world_core::WorldCore, BodyId, &ChainDef) -> ChainId,
    wrap: impl FnOnce(Arc<crate::core::world_core::WorldCore>, ChainId) -> T,
) -> T {
    let id = create(core.as_ref(), body, def);
    wrap(Arc::clone(core), id)
}

fn try_create_body_attached_chain_handle<T>(
    core: &Arc<crate::core::world_core::WorldCore>,
    body: BodyId,
    def: &ChainDef,
    create: impl FnOnce(&crate::core::world_core::WorldCore, BodyId, &ChainDef) -> ApiResult<ChainId>,
    wrap: impl FnOnce(Arc<crate::core::world_core::WorldCore>, ChainId) -> T,
) -> ApiResult<T> {
    let id = create(core.as_ref(), body, def)?;
    Ok(wrap(Arc::clone(core), id))
}

impl ChainDef {
    pub fn validate(&self) -> ApiResult<()> {
        check_chain_def_valid(self)
    }
}

impl<'w> Body<'w> {
    /// Create a chain shape attached to this body. Points/materials are cloned internally by Box2D.
    pub fn create_chain(&mut self, def: &ChainDef) -> Chain<'w> {
        create_body_attached_chain_handle(
            &self.core,
            self.id,
            def,
            create_chain_for_body_impl,
            Chain::new,
        )
    }

    pub fn try_create_chain(&mut self, def: &ChainDef) -> ApiResult<Chain<'w>> {
        try_create_body_attached_chain_handle(
            &self.core,
            self.id,
            def,
            try_create_chain_for_body_impl,
            Chain::new,
        )
    }
}

impl OwnedBody {
    /// Create a chain shape attached to this body. Points/materials are cloned internally by Box2D.
    pub fn create_chain(&mut self, def: &ChainDef) -> OwnedChain {
        create_body_attached_chain_handle(
            &self.core_arc(),
            self.id(),
            def,
            create_chain_for_body_impl,
            OwnedChain::new,
        )
    }

    pub fn try_create_chain(&mut self, def: &ChainDef) -> ApiResult<OwnedChain> {
        try_create_body_attached_chain_handle(
            &self.core_arc(),
            self.id(),
            def,
            try_create_chain_for_body_impl,
            OwnedChain::new,
        )
    }
}