Files @ e7b7d53e6952
Branch filter:

Location: CSY/reowolf/src/common.rs - annotation

e7b7d53e6952 5.1 KiB application/rls-services+xml Show Source Show as Raw Download as Raw
Christopher Esterhuyse
more tests
06f259bf8031
06f259bf8031
44a98be4e4b4
cecf94fdb875
44a98be4e4b4
06f259bf8031
06f259bf8031
4976f1816f47
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
1bacc6467d19
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
44a98be4e4b4
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
44a98be4e4b4
06f259bf8031
44a98be4e4b4
44a98be4e4b4
44a98be4e4b4
8642f7a7bf01
5c858bc28930
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
5c858bc28930
5c858bc28930
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
e7b7d53e6952
e7b7d53e6952
e7b7d53e6952
e7b7d53e6952
8642f7a7bf01
8642f7a7bf01
02eb59c6fd66
f52ad0660a73
02eb59c6fd66
44a98be4e4b4
44a98be4e4b4
44a98be4e4b4
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
3af8b6eaec1b
44a98be4e4b4
3f0b1a4ef61f
3f0b1a4ef61f
44a98be4e4b4
adb23c484182
44a98be4e4b4
44a98be4e4b4
44a98be4e4b4
06f259bf8031
06f259bf8031
06f259bf8031
44a98be4e4b4
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
06f259bf8031
44a98be4e4b4
06f259bf8031
06f259bf8031
44a98be4e4b4
44a98be4e4b4
44a98be4e4b4
06f259bf8031
06f259bf8031
06f259bf8031
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
1e7064d79bdb
1e7064d79bdb
1e7064d79bdb
1e7064d79bdb
1e7064d79bdb
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
f52ad0660a73
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
f52ad0660a73
f52ad0660a73
f52ad0660a73
f52ad0660a73
f52ad0660a73
f52ad0660a73
02eb59c6fd66
02eb59c6fd66
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
b8c262d37323
02eb59c6fd66
02eb59c6fd66
f52ad0660a73
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
02eb59c6fd66
44a98be4e4b4
0a71d0af9edf
e7b7d53e6952
e7b7d53e6952
e7b7d53e6952
e7b7d53e6952
e7b7d53e6952
e7b7d53e6952
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
8642f7a7bf01
06f259bf8031
06f259bf8031
cecf94fdb875
cecf94fdb875
cecf94fdb875
cecf94fdb875
cecf94fdb875
cecf94fdb875
cecf94fdb875
cecf94fdb875
cecf94fdb875
cecf94fdb875
///////////////////// PRELUDE /////////////////////

pub use crate::protocol::{ComponentState, ProtocolDescription};
pub use crate::runtime::{NonsyncProtoContext, SyncProtoContext};

pub use core::{
    cmp::Ordering,
    fmt::{Debug, Formatter},
    hash::{Hash, Hasher},
    ops::{Range, RangeFrom},
    time::Duration,
};
pub use indexmap::{IndexMap, IndexSet};
pub use maplit::{hashmap, hashset};
pub use mio::{
    net::{TcpListener, TcpStream},
    Events, Interest, Poll, Token,
};
pub use std::{
    collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
    convert::TryInto,
    io::{Read, Write},
    net::SocketAddr,
    sync::Arc,
    time::Instant,
};
pub use Polarity::*;

///////////////////// DEFS /////////////////////

pub type ControllerId = u32;
pub type PortSuffix = u32;

#[derive(
    Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub struct Id {
    pub(crate) controller_id: ControllerId,
    pub(crate) u32_suffix: PortSuffix,
}

#[derive(Debug, Default)]
pub struct U32Stream {
    next: u32,
}

// globally unique
#[derive(
    Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub struct PortId(Id);
#[derive(
    Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub struct FiringVar(pub(crate) PortId);
#[derive(
    Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub struct ProtoComponentId(Id);

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Payload(Arc<Vec<u8>>);

#[derive(
    Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub enum Polarity {
    Putter, // output port (from the perspective of the component)
    Getter, // input port (from the perspective of the component)
}

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum AddComponentError {
    NoSuchComponent,
    NonPortTypeParameters,
    CannotMovePort(PortId),
    WrongNumberOfParamaters { expected: usize },
    UnknownPort(PortId),
    WrongPortPolarity { port: PortId, expected_polarity: Polarity },
    DuplicateMovedPort(PortId),
}

#[derive(Debug, Clone)]
pub enum NonsyncBlocker {
    Inconsistent,
    ComponentExit,
    SyncBlockStart,
}

#[derive(Debug, Clone)]
pub enum SyncBlocker {
    Inconsistent,
    SyncBlockEnd,
    CouldntReadMsg(PortId),
    CouldntCheckFiring(PortId),
    PutMsg(PortId, Payload),
}

///////////////////// IMPL /////////////////////
impl U32Stream {
    pub fn next(&mut self) -> u32 {
        if self.next == u32::MAX {
            panic!("NO NEXT!")
        }
        self.next += 1;
        self.next - 1
    }
}
impl From<Id> for PortId {
    fn from(id: Id) -> PortId {
        Self(id)
    }
}
impl From<Id> for ProtoComponentId {
    fn from(id: Id) -> ProtoComponentId {
        Self(id)
    }
}
impl From<&[u8]> for Payload {
    fn from(s: &[u8]) -> Payload {
        Payload(Arc::new(s.to_vec()))
    }
}
impl Payload {
    pub fn new(len: usize) -> Payload {
        let mut v = Vec::with_capacity(len);
        unsafe {
            v.set_len(len);
        }
        Payload(Arc::new(v))
    }
    pub fn len(&self) -> usize {
        self.0.len()
    }
    pub fn as_slice(&self) -> &[u8] {
        &self.0
    }
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        Arc::make_mut(&mut self.0) as _
    }
    pub fn concat_with(&mut self, other: &Self) {
        let bytes = other.as_slice().iter().copied();
        let me = Arc::make_mut(&mut self.0);
        me.extend(bytes);
    }
}
impl serde::Serialize for Payload {
    fn serialize<S>(
        &self,
        serializer: S,
    ) -> std::result::Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
    where
        S: serde::Serializer,
    {
        let inner: &Vec<u8> = &self.0;
        inner.serialize(serializer)
    }
}
impl<'de> serde::Deserialize<'de> for Payload {
    fn deserialize<D>(
        deserializer: D,
    ) -> std::result::Result<Self, <D as serde::Deserializer<'de>>::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let inner: Vec<u8> = Vec::deserialize(deserializer)?;
        Ok(Self(Arc::new(inner)))
    }
}
impl std::iter::FromIterator<u8> for Payload {
    fn from_iter<I: IntoIterator<Item = u8>>(it: I) -> Self {
        Self(Arc::new(it.into_iter().collect()))
    }
}
impl From<Vec<u8>> for Payload {
    fn from(s: Vec<u8>) -> Self {
        Self(s.into())
    }
}
impl Debug for PortId {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "PID<{},{}>", self.0.controller_id, self.0.u32_suffix)
    }
}
impl Debug for FiringVar {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "VID<{},{}>", (self.0).0.controller_id, (self.0).0.u32_suffix)
    }
}
impl Debug for ProtoComponentId {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "ProtoComponentId({},{})", self.0.controller_id, self.0.u32_suffix)
    }
}
impl std::ops::Not for Polarity {
    type Output = Self;
    fn not(self) -> Self::Output {
        use Polarity::*;
        match self {
            Putter => Getter,
            Getter => Putter,
        }
    }
}