File: test_phantom_data.rs

package info (click to toggle)
rust-borsh 1.5.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,808 kB
  • sloc: makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,439 bytes parent folder | download | duplicates (13)
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
use crate::common_macro::schema_imports::*;

use core::marker::PhantomData;

#[test]
fn phantom_data_schema() {
    let phantom_declaration = PhantomData::<String>::declaration();
    assert_eq!("()", phantom_declaration);
    let phantom_declaration = PhantomData::<Vec<u8>>::declaration();
    assert_eq!("()", phantom_declaration);
}

#[test]
pub fn generic_struct_with_phantom_data_derived() {
    #[allow(unused)]
    #[derive(borsh::BorshSchema)]
    struct Parametrized<K, V> {
        field: K,
        another: PhantomData<V>,
    }

    assert_eq!(
        "Parametrized<String>".to_string(),
        <Parametrized<String, u32>>::declaration()
    );

    let mut defs = Default::default();
    <Parametrized<String, u32>>::add_definitions_recursively(&mut defs);
    assert_eq!(
        schema_map! {
            "Parametrized<String>" => Definition::Struct {
                fields: Fields::NamedFields(vec![
                    ("field".to_string(), "String".to_string()),
                    ("another".to_string(), "()".to_string())
                ])
            },
            "()" => Definition::Primitive(0),
            "String" => Definition::Sequence {
                length_width: Definition::DEFAULT_LENGTH_WIDTH,
                length_range: Definition::DEFAULT_LENGTH_RANGE,
                elements: "u8".to_string()
            },
            "u8" => Definition::Primitive(1)
        },
        defs
    );
}