File: value.rs

package info (click to toggle)
rust-glib 0.20.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,308 kB
  • sloc: makefile: 18
file content (67 lines) | stat: -rw-r--r-- 1,737 bytes parent folder | download | duplicates (3)
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
use std::ops::Deref;

use glib::{prelude::*, BoxedValue, Value};

// FIXME all .get::<i32>() should be replaced with .get(); the compiler is totally able to infer the type itself.
// But somehow without some tests are failing on Windows because the type inference doesn't work or something.

// Test that `ToValue` (and conversely, `FromValue`) uphold the promised invariants
#[test]
pub fn to_value_invariants() {
    // Inverse
    assert_eq!(0i32, 0i32.to_value().get::<i32>().unwrap());
    assert_eq!(0i32, 0i32.to_value().get::<i32>().unwrap());

    // Idempotence
    assert_eq!(
        &0i32.to_value().type_(),
        &0i32.to_value().to_value().type_()
    );
    assert_eq!(0i32, 0i32.to_value().to_value().get::<i32>().unwrap());
    assert_eq!(
        0i32,
        0i32.to_value()
            .get::<Value>()
            .unwrap()
            .get::<i32>()
            .unwrap()
    );
    assert_eq!(
        0i32,
        0i32.to_value()
            .get::<Value>()
            .unwrap()
            .get::<i32>()
            .unwrap()
    );
}

// Test that `ToValue` and `FromValue` handle nexted boxed values correctly (as per the documentation)
#[test]
pub fn to_value_boxed() {
    let x = 0i32.to_value();
    let boxed = BoxedValue(x);
    assert_eq!(
        0i32,
        boxed
            .to_value()
            .to_value()
            .get::<BoxedValue>()
            .unwrap()
            .deref()
            .get::<i32>()
            .unwrap()
    );
    assert_eq!(
        0i32,
        boxed
            .to_value()
            .get::<Value>()
            .unwrap()
            .get::<BoxedValue>()
            .unwrap()
            .deref()
            .get::<i32>()
            .unwrap()
    );
}