use bitflags::bitflags;
use carla_sys::carla::rpc::VehicleLightState_LightState as FfiVehicleLightState;
bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct VehicleLightState: u32 {
const NONE = 0x0;
const POSITION = 0x1;
const LOW_BEAM = 0x2;
const HIGH_BEAM = 0x4;
const BRAKE = 0x8;
const RIGHT_BLINKER = 0x10;
const LEFT_BLINKER = 0x20;
const REVERSE = 0x40;
const FOG = 0x80;
const INTERIOR = 0x100;
const SPECIAL1 = 0x200;
const SPECIAL2 = 0x400;
const ALL = 0xFFFFFFFF;
}
}
impl VehicleLightState {
pub(crate) fn to_ffi(self) -> FfiVehicleLightState {
unsafe {
let mut uninit = std::mem::MaybeUninit::<FfiVehicleLightState>::uninit();
std::ptr::write(uninit.as_mut_ptr() as *mut u32, self.bits());
uninit.assume_init()
}
}
pub(crate) fn from_ffi(ffi: &FfiVehicleLightState) -> Self {
unsafe {
let bits = std::ptr::read(ffi as *const _ as *const u32);
VehicleLightState::from_bits_truncate(bits)
}
}
}
impl From<VehicleLightState> for FfiVehicleLightState {
fn from(state: VehicleLightState) -> Self {
state.to_ffi()
}
}
impl From<&VehicleLightState> for FfiVehicleLightState {
fn from(state: &VehicleLightState) -> Self {
(*state).to_ffi()
}
}
impl From<FfiVehicleLightState> for VehicleLightState {
fn from(ffi: FfiVehicleLightState) -> Self {
VehicleLightState::from_ffi(&ffi)
}
}
impl From<&FfiVehicleLightState> for VehicleLightState {
fn from(ffi: &FfiVehicleLightState) -> Self {
VehicleLightState::from_ffi(ffi)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bitflags_operations() {
let lights = VehicleLightState::POSITION | VehicleLightState::LOW_BEAM;
assert!(lights.contains(VehicleLightState::POSITION));
assert!(lights.contains(VehicleLightState::LOW_BEAM));
assert!(!lights.contains(VehicleLightState::HIGH_BEAM));
let lights = lights ^ VehicleLightState::HIGH_BEAM;
assert!(lights.contains(VehicleLightState::HIGH_BEAM));
let lights = lights & !VehicleLightState::POSITION;
assert!(!lights.contains(VehicleLightState::POSITION));
assert!(lights.contains(VehicleLightState::LOW_BEAM));
}
#[test]
fn test_bit_values() {
assert_eq!(VehicleLightState::NONE.bits(), 0x0);
assert_eq!(VehicleLightState::POSITION.bits(), 0x1);
assert_eq!(VehicleLightState::LOW_BEAM.bits(), 0x2);
assert_eq!(VehicleLightState::HIGH_BEAM.bits(), 0x4);
assert_eq!(VehicleLightState::BRAKE.bits(), 0x8);
assert_eq!(VehicleLightState::RIGHT_BLINKER.bits(), 0x10);
assert_eq!(VehicleLightState::LEFT_BLINKER.bits(), 0x20);
assert_eq!(VehicleLightState::REVERSE.bits(), 0x40);
assert_eq!(VehicleLightState::FOG.bits(), 0x80);
assert_eq!(VehicleLightState::INTERIOR.bits(), 0x100);
assert_eq!(VehicleLightState::SPECIAL1.bits(), 0x200);
assert_eq!(VehicleLightState::SPECIAL2.bits(), 0x400);
assert_eq!(VehicleLightState::ALL.bits(), 0xFFFFFFFF);
}
#[test]
fn test_from_bits() {
let lights = VehicleLightState::from_bits_truncate(0x3);
assert!(lights.contains(VehicleLightState::POSITION));
assert!(lights.contains(VehicleLightState::LOW_BEAM));
}
}