Files
@ 6ec2e0261a03
Branch filter:
Location: CSY/reowolf/src/common.rs
6ec2e0261a03
6.8 KiB
application/rls-services+xml
debugging type inference
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | ///////////////////// PRELUDE /////////////////////
pub(crate) use crate::protocol::{ComponentState, ProtocolDescription};
pub(crate) use crate::runtime::{error::AddComponentError, NonsyncProtoContext, SyncProtoContext};
pub(crate) use core::{
cmp::Ordering,
fmt::{Debug, Formatter},
hash::Hash,
ops::Range,
time::Duration,
};
pub(crate) use maplit::hashmap;
pub(crate) use mio::{
net::{TcpListener, TcpStream},
Events, Interest, Poll, Token,
};
pub(crate) use std::{
collections::{BTreeMap, HashMap, HashSet},
convert::TryInto,
io::{Read, Write},
net::SocketAddr,
sync::Arc,
time::Instant,
};
pub(crate) use Polarity::*;
pub(crate) trait IdParts {
fn id_parts(self) -> (ConnectorId, U32Suffix);
}
/// Used by various distributed algorithms to identify connectors.
pub type ConnectorId = u32;
/// Used in conjunction with the `ConnectorId` type to create identifiers for ports and components
pub type U32Suffix = u32;
#[derive(
Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize,
)]
/// Generalization of a port/component identifier
#[repr(C)]
pub struct Id {
pub(crate) connector_id: ConnectorId,
pub(crate) u32_suffix: U32Suffix,
}
#[derive(Clone, Debug, Default)]
pub struct U32Stream {
next: u32,
}
/// Identifier of a component in a session
#[derive(
Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub struct ComponentId(Id); // PUB because it can be returned by errors
/// Identifier of a port in a session
#[derive(
Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize,
)]
#[repr(transparent)]
pub struct PortId(Id);
/// A safely aliasable heap-allocated payload of message bytes
#[derive(Default, Eq, PartialEq, Clone, Ord, PartialOrd)]
pub struct Payload(Arc<Vec<u8>>);
#[derive(
Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd, serde::Serialize, serde::Deserialize,
)]
/// "Orientation" of a port, determining whether they can send or receive messages with `put` and `get` respectively.
#[repr(C)]
pub enum Polarity {
Putter, // output port (from the perspective of the component)
Getter, // input port (from the perspective of the component)
}
#[derive(
Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd, serde::Serialize, serde::Deserialize,
)]
/// "Orientation" of a transport-layer network endpoint, dictating how it's connection procedure should
/// be conducted. Corresponds with connect() / accept() familiar to TCP socket programming.
#[repr(C)]
pub enum EndpointPolarity {
Active, // calls connect()
Passive, // calls bind() listen() accept()
}
#[derive(Debug, Clone)]
pub(crate) enum NonsyncBlocker {
Inconsistent,
ComponentExit,
SyncBlockStart,
}
#[derive(Debug, Clone)]
pub(crate) enum SyncBlocker {
Inconsistent,
SyncBlockEnd,
CouldntReadMsg(PortId),
CouldntCheckFiring(PortId),
PutMsg(PortId, Payload),
}
pub(crate) struct DenseDebugHex<'a>(pub &'a [u8]);
pub(crate) struct DebuggableIter<I: Iterator<Item = T> + Clone, T: Debug>(pub(crate) I);
///////////////////// IMPL /////////////////////
impl IdParts for Id {
fn id_parts(self) -> (ConnectorId, U32Suffix) {
(self.connector_id, self.u32_suffix)
}
}
impl IdParts for PortId {
fn id_parts(self) -> (ConnectorId, U32Suffix) {
self.0.id_parts()
}
}
impl IdParts for ComponentId {
fn id_parts(self) -> (ConnectorId, U32Suffix) {
self.0.id_parts()
}
}
impl U32Stream {
pub(crate) fn next(&mut self) -> u32 {
if self.next == u32::MAX {
panic!("NO NEXT!")
}
self.next += 1;
self.next - 1
}
pub(crate) fn n_skipped(mut self, n: u32) -> Self {
self.next = self.next.saturating_add(n);
self
}
}
impl From<Id> for PortId {
fn from(id: Id) -> PortId {
Self(id)
}
}
impl From<Id> for ComponentId {
fn from(id: Id) -> Self {
Self(id)
}
}
impl From<&[u8]> for Payload {
fn from(s: &[u8]) -> Payload {
Payload(Arc::new(s.to_vec()))
}
}
impl Payload {
/// Create a new payload of uninitialized bytes with the given length.
pub fn new(len: usize) -> Payload {
let mut v = Vec::with_capacity(len);
unsafe {
v.set_len(len);
}
Payload(Arc::new(v))
}
/// Returns the length of the payload's byte sequence
pub fn len(&self) -> usize {
self.0.len()
}
/// Allows shared reading of the payload's contents
pub fn as_slice(&self) -> &[u8] {
&self.0
}
/// Allows mutation of the payload's contents.
/// Results in a deep copy in the event this payload is aliased.
pub fn as_mut_vec(&mut self) -> &mut Vec<u8> {
Arc::make_mut(&mut self.0)
}
/// Modifies this payload, concatenating the given immutable payload's contents.
/// Results in a deep copy in the event this payload is aliased.
pub fn concatenate_with(&mut self, other: &Self) {
let bytes = other.as_slice().iter().copied();
let me = self.as_mut_vec();
me.extend(bytes);
}
}
impl serde::Serialize for Payload {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::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) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let inner: Vec<u8> = Vec::deserialize(deserializer)?;
Ok(Self(Arc::new(inner)))
}
}
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 {
let (a, b) = self.id_parts();
write!(f, "pid{}_{}", a, b)
}
}
impl Debug for ComponentId {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
let (a, b) = self.id_parts();
write!(f, "cid{}_{}", a, b)
}
}
impl Debug for Payload {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Payload[{:?}]", DenseDebugHex(self.as_slice()))
}
}
impl std::ops::Not for Polarity {
type Output = Self;
fn not(self) -> Self::Output {
use Polarity::*;
match self {
Putter => Getter,
Getter => Putter,
}
}
}
impl Debug for DenseDebugHex<'_> {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
for b in self.0 {
write!(f, "{:02X?}", b)?;
}
Ok(())
}
}
impl<I: Iterator<Item = T> + Clone, T: Debug> Debug for DebuggableIter<I, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_list().entries(self.0.clone()).finish()
}
}
|