sel4_shared_ring_buffer/
roles.rs

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use crate::InitializationStrategy;

pub enum Provide {}
pub enum Use {}

pub trait RingBuffersRole: RingBuffersRoleSealed {
    type FreeRole: RingBufferRole;
    type UsedRole: RingBufferRole;

    const ROLE: RingBuffersRoleValue;

    fn default_initialization_strategy() -> InitializationStrategy;
}

impl RingBuffersRole for Provide {
    type FreeRole = Write;
    type UsedRole = Read;

    const ROLE: RingBuffersRoleValue = RingBuffersRoleValue::Provide;

    fn default_initialization_strategy() -> InitializationStrategy {
        InitializationStrategy::UseAndWriteState(Default::default())
    }
}

impl RingBuffersRole for Use {
    type FreeRole = Read;
    type UsedRole = Write;

    const ROLE: RingBuffersRoleValue = RingBuffersRoleValue::Use;

    fn default_initialization_strategy() -> InitializationStrategy {
        InitializationStrategy::ReadState
    }
}

#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum RingBuffersRoleValue {
    Provide,
    Use,
}

impl RingBuffersRoleValue {
    pub fn is_provide(self) -> bool {
        self == Self::Provide
    }

    pub fn is_use(self) -> bool {
        self == Self::Use
    }
}

pub enum Write {}
pub enum Read {}

pub trait RingBufferRole: RingBufferRoleSealed {
    const ROLE: RingBufferRoleValue;
}

impl RingBufferRole for Write {
    const ROLE: RingBufferRoleValue = RingBufferRoleValue::Write;
}

impl RingBufferRole for Read {
    const ROLE: RingBufferRoleValue = RingBufferRoleValue::Read;
}

#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum RingBufferRoleValue {
    Write,
    Read,
}

impl RingBufferRoleValue {
    pub fn is_write(self) -> bool {
        self == Self::Write
    }

    pub fn is_read(self) -> bool {
        self == Self::Read
    }
}

use sealing::{RingBufferRoleSealed, RingBuffersRoleSealed};

mod sealing {
    use super::*;

    pub trait RingBuffersRoleSealed {}

    impl RingBuffersRoleSealed for Provide {}
    impl RingBuffersRoleSealed for Use {}

    pub trait RingBufferRoleSealed {}

    impl RingBufferRoleSealed for Write {}
    impl RingBufferRoleSealed for Read {}
}