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
68
69
70
71
72
73
74
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Joining of flat layouts of different variants of a `variant` type.
//!
//! When flattening `variant` types, a single flat layout must be obtained for the type by joining
//! the flat layout of each variant. This means finding a flat type for each layout element to
//! represent the flat type of any of the variants. See [`crate::primitive_types::JoinFlatTypes`]
//! for more information on how flat types are joined.

use either::Either;
use frunk::{HCons, HNil};

use crate::primitive_types::{FlatType, JoinFlatTypes};

/// Allows converting between the current flat layout and the joined `Target` flat layout, which
/// may be longer or have some elements wider than the current elements.
pub trait JoinFlatLayouts<Target> {
    /// Converts the current flat layout into a the joined `Target` flat layout.
    fn into_joined(self) -> Target;

    /// Converts from the joined `Target` flat layout into the current flat layout.
    fn from_joined(joined: Target) -> Self;
}

impl JoinFlatLayouts<HNil> for HNil {
    fn into_joined(self) -> HNil {
        HNil
    }

    fn from_joined(_joined: HNil) -> Self {
        HNil
    }
}

impl<TargetHead, TargetTail> JoinFlatLayouts<HCons<TargetHead, TargetTail>> for HNil
where
    TargetHead: Default,
    HNil: JoinFlatLayouts<TargetTail>,
{
    fn into_joined(self) -> HCons<TargetHead, TargetTail> {
        HCons {
            head: TargetHead::default(),
            tail: HNil.into_joined(),
        }
    }

    fn from_joined(_joined: HCons<TargetHead, TargetTail>) -> Self {
        HNil
    }
}

impl<SourceHead, SourceTail, TargetHead, TargetTail> JoinFlatLayouts<HCons<TargetHead, TargetTail>>
    for HCons<SourceHead, SourceTail>
where
    SourceHead: FlatType,
    TargetHead: FlatType,
    Either<SourceHead, TargetHead>: JoinFlatTypes<Flat = TargetHead>,
    SourceTail: JoinFlatLayouts<TargetTail>,
{
    fn into_joined(self) -> HCons<TargetHead, TargetTail> {
        HCons {
            head: Either::Left(self.head).join(),
            tail: self.tail.into_joined(),
        }
    }

    fn from_joined(joined: HCons<TargetHead, TargetTail>) -> Self {
        HCons {
            head: joined.head.split_into(),
            tail: SourceTail::from_joined(joined.tail),
        }
    }
}