carla 0.14.1

Rust client library for Carla simulator
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
use core::slice;

use super::{Junction, Landmark, LandmarkList, Waypoint, WaypointList};
use crate::{
    error::ffi::with_ffi_error,
    geom::{GeoLocation, Location, Transform},
    road::{LaneId, LaneType, RoadId},
};
use autocxx::WithinUniquePtr;
use carla_sys::carla_rust::client::{FfiMap, FfiTransformList};
use cxx::{SharedPtr, UniquePtr};
use derivative::Derivative;
use static_assertions::assert_impl_all;

/// Represents the map of the simulation.
///
/// Corresponds to [`carla.Map`](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map) in the Python API.
///
/// The [`Map`] provides access to the road network based on the OpenDRIVE standard.
/// It allows you to query waypoints, spawn points, landmarks, and road topology.
///
/// # Key Features
///
/// - **Navigation**: Generate waypoints for path planning
/// - **Spawn points**: Get recommended vehicle spawn locations
/// - **Road network**: Query road topology, lanes, and junctions
/// - **Landmarks**: Access traffic signs, lights, and road markings
/// - **OpenDRIVE**: Export raw OpenDRIVE XML description
///
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use carla::{client::Client, geom::Location};
///
/// let client = Client::connect("localhost", 2000, None)?;
/// let world = client.world()?;
/// let map = world.map()?;
///
/// // Get map name
/// println!("Map: {}", map.name());
///
/// // Get recommended spawn points
/// let spawn_points = map.recommended_spawn_points()?;
/// if let Some(spawn_point) = spawn_points.get(0) {
///     println!("First spawn point: {:?}", spawn_point);
/// }
///
/// // Generate waypoints for navigation (every 2 meters)
/// let waypoints = map.generate_waypoints(2.0)?;
/// println!("Generated {} waypoints", waypoints.len());
///
/// // Find nearest waypoint to a location
/// let location = Location::new(100.0, 200.0, 1.0);
/// if let Some(waypoint) = map.waypoint_at(&location)? {
///     println!("Nearest waypoint on lane {}", waypoint.lane_id());
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Derivative)]
#[derivative(Debug)]
#[repr(transparent)]
pub struct Map {
    #[derivative(Debug = "ignore")]
    inner: SharedPtr<FfiMap>,
}

impl Map {
    /// Returns the name of the map (e.g., "Town01", "Town02").
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.name](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.name)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.name](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.name)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.name](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.name)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    pub fn name(&self) -> String {
        self.inner.GetName().to_string()
    }

    /// Returns the raw OpenDRIVE XML description of the map.
    ///
    /// OpenDRIVE is the standard format for road network descriptions.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.to_opendrive](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.to_opendrive)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.to_opendrive](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.to_opendrive)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.to_opendrive](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.to_opendrive)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    pub fn to_open_drive(&self) -> String {
        self.inner.GetOpenDrive().to_string()
    }

    /// Returns recommended spawn points for vehicles.
    ///
    /// These are predefined locations suitable for spawning vehicles without collisions.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_spawn_points](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_spawn_points)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_spawn_points](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_spawn_points)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_spawn_points](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_spawn_points)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    pub fn recommended_spawn_points(&self) -> crate::Result<RecommendedSpawnPoints> {
        let ptr = with_ffi_error("recommended_spawn_points", |e| {
            self.inner.GetRecommendedSpawnPoints(e).within_unique_ptr()
        })?;
        Ok(unsafe { RecommendedSpawnPoints::from_cxx(ptr).unwrap_unchecked() })
    }

    /// Finds the nearest waypoint on a driving lane.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_waypoint](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_waypoint)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_waypoint](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_waypoint)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_waypoint](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_waypoint)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Arguments
    ///
    /// * `location` - The 3D position to search from
    ///
    /// # Returns
    ///
    /// The nearest waypoint on a driving lane, or `None` if no waypoint is found.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # use carla::client::Client;
    /// # use carla::geom::Location;
    /// let client = Client::connect("localhost", 2000, None)?;
    /// let world = client.world()?;
    /// let map = world.map()?;
    ///
    /// let location = Location::new(100.0, 50.0, 0.5);
    /// if let Some(waypoint) = map.waypoint_at(&location)? {
    ///     println!("Found waypoint at road {}", waypoint.road_id());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn waypoint_at(&self, location: &Location) -> crate::Result<Option<Waypoint>> {
        let ptr = with_ffi_error("waypoint_at", |e| {
            self.inner
                .GetWaypoint(location.as_ffi(), true, LaneType::Driving as i32, e)
        })?;
        Ok(Waypoint::from_cxx(ptr))
    }

    /// Finds the nearest waypoint with custom options.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_waypoint](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_waypoint)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_waypoint](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_waypoint)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_waypoint](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_waypoint)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Arguments
    ///
    /// * `location` - 3D position to query from
    /// * `project_to_road` - If true, projects the location onto the road surface
    /// * `lane_type` - Filter by lane type (driving, parking, sidewalk, etc.)
    ///
    /// # Returns
    ///
    /// The nearest waypoint matching the criteria, or `None` if not found.
    pub fn waypoint_opt(
        &self,
        location: &Location,
        project_to_road: bool,
        lane_type: LaneType,
    ) -> crate::Result<Option<Waypoint>> {
        let ptr = with_ffi_error("waypoint_opt", |e| {
            self.inner
                .GetWaypoint(location.as_ffi(), project_to_road, lane_type as i32, e)
        })?;
        Ok(Waypoint::from_cxx(ptr))
    }

    /// Gets a waypoint by OpenDRIVE coordinates.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_waypoint_xodr](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_waypoint_xodr)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_waypoint_xodr](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_waypoint_xodr)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_waypoint_xodr](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_waypoint_xodr)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Arguments
    ///
    /// * `road_id` - Road identifier from OpenDRIVE
    /// * `lane_id` - Lane identifier (negative for right lanes, positive for left)
    /// * `distance` - Distance along the road (meters from road start)
    pub fn waypoint_xodr(
        &self,
        road_id: RoadId,
        lane_id: LaneId,
        distance: f32,
    ) -> crate::Result<Option<Waypoint>> {
        let ptr = with_ffi_error("waypoint_xodr", |e| {
            self.inner
                .GetWaypointXODR(road_id.into(), lane_id.into(), distance, e)
        })?;
        Ok(Waypoint::from_cxx(ptr))
    }

    /// Generates a grid of waypoints covering the entire road network.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.generate_waypoints](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.generate_waypoints)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.generate_waypoints](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.generate_waypoints)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.generate_waypoints](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.generate_waypoints)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Arguments
    ///
    /// * `distance` - Approximate distance between waypoints (meters)
    ///
    /// # Returns
    ///
    /// A list of waypoints covering all drivable lanes.
    pub fn generate_waypoints(&self, distance: f64) -> crate::Result<WaypointList> {
        let waypoints = with_ffi_error("generate_waypoints", |e| {
            self.inner.GenerateWaypoints(distance, e)
        })?;
        Ok(unsafe { WaypointList::from_cxx(waypoints).unwrap_unchecked() })
    }

    /// Gets the junction associated with a waypoint.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_junction](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_junction)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_junction](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_junction)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_junction](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_junction)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Arguments
    ///
    /// * `waypoint` - Waypoint to query (should be inside a junction)
    pub fn junction(&self, waypoint: &Waypoint) -> crate::Result<Junction> {
        let junction = with_ffi_error("junction", |e| self.inner.GetJunction(&waypoint.inner, e))?;
        Ok(unsafe { Junction::from_cxx(junction).unwrap_unchecked() })
    }

    /// Returns all landmarks in the map (traffic signs, lights, etc.).
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_all_landmarks](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_all_landmarks)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_all_landmarks](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_all_landmarks)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_all_landmarks](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_all_landmarks)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    pub fn all_landmarks(&self) -> crate::Result<LandmarkList> {
        let ptr = with_ffi_error("all_landmarks", |e| self.inner.GetAllLandmarks(e))?;
        Ok(unsafe { LandmarkList::from_cxx(ptr).unwrap_unchecked() })
    }

    /// Finds landmarks by their OpenDRIVE ID.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_landmark_from_id](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_landmark_from_id)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_landmark_from_id](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_landmark_from_id)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_landmark_from_id](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_landmark_from_id)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Arguments
    ///
    /// * `id` - Landmark identifier from OpenDRIVE
    pub fn landmarks_from_id(&self, id: &str) -> crate::Result<LandmarkList> {
        let ptr = with_ffi_error("landmarks_from_id", |e| {
            self.inner.GetLandmarksFromId(id, e)
        })?;
        Ok(unsafe { LandmarkList::from_cxx(ptr).unwrap_unchecked() })
    }

    /// Finds all landmarks of a specific type.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_all_landmarks_of_type](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_all_landmarks_of_type)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_all_landmarks_of_type](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_all_landmarks_of_type)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_all_landmarks_of_type](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_all_landmarks_of_type)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Arguments
    ///
    /// * `type_` - Landmark type (e.g., "1000001" for speed limit signs)
    pub fn all_landmarks_of_type(&self, type_: &str) -> crate::Result<LandmarkList> {
        let ptr = with_ffi_error("all_landmarks_of_type", |e| {
            self.inner.GetAllLandmarksOfType(type_, e)
        })?;
        Ok(unsafe { LandmarkList::from_cxx(ptr).unwrap_unchecked() })
    }

    /// Gets all landmarks in the same group as the given landmark.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_landmark_group](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_landmark_group)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_landmark_group](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_landmark_group)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_landmark_group](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_landmark_group)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Arguments
    ///
    /// * `landmark` - Reference landmark
    pub fn landmark_group(&self, landmark: &Landmark) -> crate::Result<LandmarkList> {
        let ptr = with_ffi_error("landmark_group", |e| {
            self.inner.GetLandmarkGroup(&landmark.inner, e)
        })?;
        Ok(unsafe { LandmarkList::from_cxx(ptr).unwrap_unchecked() })
    }

    /// Returns the road network topology as pairs of connected waypoints.
    ///
    /// The topology describes the connectivity of the road network, where each
    /// pair represents a connection from one waypoint to another. This is useful
    /// for path planning and understanding the road network structure.
    #[cfg_attr(
        carla_version_0916,
        doc = " See [carla.Map.get_topology](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Map.get_topology)"
    )]
    #[cfg_attr(
        carla_version_0915,
        doc = " See [carla.Map.get_topology](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Map.get_topology)"
    )]
    #[cfg_attr(
        carla_version_0914,
        doc = " See [carla.Map.get_topology](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Map.get_topology)"
    )]
    #[cfg_attr(
        any(carla_version_0916, carla_version_0915, carla_version_0914),
        doc = " in the Python API."
    )]
    ///
    /// # Returns
    ///
    /// A vector of waypoint pairs, where each pair `(start, end)` represents
    /// a directed connection in the road network.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use carla::client::Client;
    ///
    /// let client = Client::connect("localhost", 2000, None)?;
    /// let world = client.world()?;
    /// let map = world.map()?;
    ///
    /// let topology = map.topology()?;
    /// println!("Road network has {} connections", topology.len());
    ///
    /// // Examine first connection
    /// if let Some((start, end)) = topology.first() {
    ///     println!(
    ///         "Connection: lane {} -> lane {}",
    ///         start.lane_id(),
    ///         end.lane_id()
    ///     );
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn topology(&self) -> crate::Result<Vec<(Waypoint, Waypoint)>> {
        let topology_pairs = with_ffi_error("topology", |e| self.inner.GetTopology(e))?;
        let mut result = Vec::with_capacity(topology_pairs.len());

        for pair in topology_pairs.iter() {
            let first = pair.first();
            let second = pair.second();

            // Skip null waypoints in topology
            if let (Some(start), Some(end)) =
                (Waypoint::from_cxx(first), Waypoint::from_cxx(second))
            {
                result.push((start, end));
            }
        }

        Ok(result)
    }

    /// Returns the geo-reference of the map (latitude, longitude, altitude origin).
    pub fn geo_reference(&self) -> crate::Result<GeoLocation> {
        let geo = with_ffi_error("geo_reference", |e| self.inner.GetGeoReference(e))?;
        Ok(GeoLocation::from_ffi(geo))
    }

    /// Returns all crosswalk zone locations in the map.
    pub fn crosswalk_zones(&self) -> crate::Result<Vec<Location>> {
        let zones = with_ffi_error("crosswalk_zones", |e| self.inner.GetAllCrosswalkZones(e))?;
        Ok(zones
            .iter()
            .map(|loc| Location::new(loc.x, loc.y, loc.z))
            .collect())
    }

    /// Generates a chunked mesh for the map and saves it to disk.
    ///
    /// This pre-processes the map geometry for faster loading.
    pub fn cook_in_memory_map(&self, path: &str) -> crate::Result<()> {
        with_ffi_error("cook_in_memory_map", |e| {
            self.inner.CookInMemoryMap(path.to_string(), e)
        })
    }
}

impl Map {
    pub fn from_cxx(ptr: SharedPtr<FfiMap>) -> Option<Self> {
        if ptr.is_null() {
            None
        } else {
            Some(Self { inner: ptr })
        }
    }
}

/// List of recommended spawn points for vehicles.
///
/// These are predefined transforms (position + rotation) that are suitable
/// for spawning vehicles without immediate collisions.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use carla::client::Client;
///
/// let client = Client::connect("localhost", 2000, None)?;
/// let world = client.world()?;
/// let map = world.map()?;
///
/// let spawn_points = map.recommended_spawn_points()?;
/// println!("Available spawn points: {}", spawn_points.len());
///
/// // Use the first spawn point
/// if let Some(transform) = spawn_points.get(0) {
///     println!("Spawn at: {:?}", transform.location);
/// }
///
/// // Iterate over all spawn points
/// for (i, transform) in spawn_points.iter().enumerate().take(5) {
///     println!("Spawn point {}: {:?}", i, transform);
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Derivative)]
#[derivative(Debug)]
#[repr(transparent)]
pub struct RecommendedSpawnPoints {
    #[derivative(Debug = "ignore")]
    inner: UniquePtr<FfiTransformList>,
}

impl RecommendedSpawnPoints {
    /// Returns the number of spawn points.
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns true if there are no spawn points.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the spawn points as a slice.
    pub fn as_slice(&self) -> &[Transform] {
        // Safety: Transform is #[repr(transparent)] over FfiTransform,
        // so pointer cast is safe
        unsafe { slice::from_raw_parts(self.inner.data() as *const Transform, self.len()) }
    }

    /// Gets the spawn point at the given index.
    ///
    /// Returns `None` if the index is out of bounds.
    pub fn get(&self, index: usize) -> Option<&Transform> {
        self.as_slice().get(index)
    }

    /// Returns an iterator over all spawn points.
    pub fn iter(&self) -> impl Iterator<Item = &Transform> + Send + '_ {
        self.as_slice().iter()
    }

    fn from_cxx(ptr: UniquePtr<FfiTransformList>) -> Option<Self> {
        if ptr.is_null() {
            None
        } else {
            Some(Self { inner: ptr })
        }
    }
}

assert_impl_all!(Map: Send, Sync);

assert_impl_all!(RecommendedSpawnPoints: Send, Sync);