Skip to main content

libobs_wrapper\graphics/
vec2.rs

1// The `getters0::Getters` derive expands to `Self { x: x, .. }` with the field spans, which
2// clippy >= 1.99 reports as `redundant_field_names` in this module.
3#![allow(clippy::redundant_field_names)]
4
5use getters0::Getters;
6
7#[derive(Debug, Clone, Copy, PartialEq, Getters)]
8pub struct Vec2 {
9    #[get_mut]
10    x: f32,
11    #[get_mut]
12    y: f32,
13}
14
15impl From<libobs::vec2> for Vec2 {
16    fn from(raw: libobs::vec2) -> Self {
17        // SAFETY: libobs::vec2 is a C representation with a known layout, the __bindgen_anon_1 must be set
18        let inner = unsafe { raw.__bindgen_anon_1.__bindgen_anon_1 };
19        Self {
20            x: inner.x,
21            y: inner.y,
22        }
23    }
24}
25
26impl From<Vec2> for libobs::vec2 {
27    fn from(val: Vec2) -> Self {
28        libobs::vec2 {
29            __bindgen_anon_1: libobs::vec2__bindgen_ty_1 {
30                __bindgen_anon_1: libobs::vec2__bindgen_ty_1__bindgen_ty_1 { x: val.x, y: val.y },
31            },
32        }
33    }
34}
35
36#[test]
37fn test_vec2() {
38    let vec_val = Vec2::new(1.0, 2.0);
39    let libobs_vec: libobs::vec2 = vec_val.into();
40
41    let original = Vec2::from(libobs_vec);
42    assert_eq!(original.x, 1.0);
43    assert_eq!(original.y, 2.0);
44    assert_ne!(original.x, 0.0);
45    assert_ne!(original.y, 0.0);
46}
47
48#[test]
49fn test_vec2_new() {
50    let vec = Vec2::new(3.5, 4.5);
51    assert_eq!(vec.x, 3.5);
52    assert_eq!(vec.y, 4.5);
53}
54
55#[test]
56fn test_vec2_clone() {
57    let vec1 = Vec2::new(1.0, 2.0);
58    #[allow(clippy::clone_on_copy)]
59    let vec2 = vec1.clone();
60    assert_eq!(vec1.x, vec2.x);
61    assert_eq!(vec1.y, vec2.y);
62}
63
64#[test]
65fn test_vec2_copy() {
66    let vec1 = Vec2::new(1.0, 2.0);
67    let vec2 = vec1; // Copy, not move
68    assert_eq!(vec1.x, vec2.x);
69    assert_eq!(vec1.y, vec2.y);
70}
71
72#[test]
73fn test_vec2_debug() {
74    let vec = Vec2::new(1.0, 2.0);
75    let debug_str = format!("{:?}", vec);
76    assert!(debug_str.contains("Vec2"));
77}