From 9b5556aa69af65dc8fbca5692b890bb3c976e5b1 Mon Sep 17 00:00:00 2001 From: Franco Cipollone <53065142+francocipollone@users.noreply.github.com> Date: Tue, 14 May 2024 15:06:16 -0300 Subject: [PATCH] RoadGeometryt::get_lane returns an Option. (#97) Signed-off-by: Franco Cipollone --- maliput/src/api/mod.rs | 22 +++++++++++++--------- maliput/tests/lane_test.rs | 4 ++-- maliput/tests/road_geometry_test.rs | 3 ++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/maliput/src/api/mod.rs b/maliput/src/api/mod.rs index dd9c052..a1261d4 100644 --- a/maliput/src/api/mod.rs +++ b/maliput/src/api/mod.rs @@ -109,15 +109,19 @@ impl<'a> RoadGeometry<'a> { } } /// Get the lane matching given `lane_id`. - pub fn get_lane(&self, lane_id: &String) -> Lane { - unsafe { - Lane { - lane: maliput_sys::api::ffi::RoadGeometry_GetLane(self.rg, lane_id) - .lane - .as_ref() - .expect(""), - } - } + /// ### Arguments + /// * `lane_id` - The id of the lane. + /// ### Return + /// The lane with the given id. + /// If no lane is found with the given id, return None. + pub fn get_lane(&self, lane_id: &String) -> Option { + let lane = maliput_sys::api::ffi::RoadGeometry_GetLane(self.rg, lane_id); + if lane.lane.is_null() { + return None; + } + Some(Lane { + lane: unsafe { lane.lane.as_ref().expect("") }, + }) } /// Get all lanes of the `RoadGeometry`. /// Returns a vector of `Lane`. diff --git a/maliput/tests/lane_test.rs b/maliput/tests/lane_test.rs index 650f02a..1c22175 100644 --- a/maliput/tests/lane_test.rs +++ b/maliput/tests/lane_test.rs @@ -103,8 +103,8 @@ fn lane_end_api_test() { let road_geometry = road_network.road_geometry(); let lane_id = String::from("0_0_1"); - let lane_end_start = maliput::api::LaneEnd::Start(road_geometry.get_lane(&lane_id)); - let lane_end_end = maliput::api::LaneEnd::Finish(road_geometry.get_lane(&lane_id)); + let lane_end_start = maliput::api::LaneEnd::Start(road_geometry.get_lane(&lane_id).unwrap()); + let lane_end_end = maliput::api::LaneEnd::Finish(road_geometry.get_lane(&lane_id).unwrap()); assert_eq!(&lane_end_start, &lane_end_start); assert_ne!(&lane_end_start, &lane_end_end); match lane_end_start { diff --git a/maliput/tests/road_geometry_test.rs b/maliput/tests/road_geometry_test.rs index 18a3d12..bb79c23 100644 --- a/maliput/tests/road_geometry_test.rs +++ b/maliput/tests/road_geometry_test.rs @@ -72,7 +72,8 @@ fn by_index() { let road_geometry = road_network.road_geometry(); let lane_id = String::from("0_0_1"); let lane = road_geometry.get_lane(&lane_id); - assert_eq!(lane.id(), "0_0_1"); + assert!(lane.is_some()); + assert_eq!(lane.unwrap().id(), "0_0_1"); let lanes = road_geometry.get_lanes(); assert_eq!(lanes.len(), 12);