From 1410a137843e0d4818ecc14e343b16f6f99a3976 2020-02-05 17:26:58 From: Christopher Esterhuyse Date: 2020-02-05 17:26:58 Subject: [PATCH] api --- diff --git a/src/runtime/connector.rs b/src/runtime/connector.rs index 149f5249ca3512fbc780c932b8256afbc8d18ae3..52acf3b112aceaac4c5b9aaf725cfdc56c5dd26d 100644 --- a/src/runtime/connector.rs +++ b/src/runtime/connector.rs @@ -29,12 +29,8 @@ impl Connector { Connector::Unconfigured(Unconfigured { controller_id }) => *controller_id, }; let protocol_description = Arc::new(ProtocolD::parse(pdl).map_err(ParseErr)?); - let proto_maybe_bindings = protocol_description - .main_interface_polarities() - .into_iter() - .zip(std::iter::repeat(None)) - .collect(); - let configured = Configured { controller_id, protocol_description, proto_maybe_bindings }; + let configured = + Configured { controller_id, protocol_description, bindings: Default::default() }; *self = Connector::Configured(configured); Ok(()) } @@ -50,14 +46,8 @@ impl Connector { Connector::Unconfigured { .. } => Err(NotConfigured), Connector::Connected(_) => Err(AlreadyConnected), Connector::Configured(configured) => { - match configured.proto_maybe_bindings.get_mut(proto_port_index) { - None => Err(IndexOutOfBounds), - Some((_polarity, Some(_))) => Err(PortAlreadyBound), - Some((_polarity, x @ None)) => { - *x = Some(binding); - Ok(()) - } - } + configured.bindings.insert(proto_port_index, binding); + Ok(()) } } } @@ -70,7 +60,14 @@ impl Connector { Connector::Configured(configured) => configured, }; // 1. Unwrap bindings or err - let bound_proto_interface: Vec<(_, _)> = configured + let mut bindings_vec = Vec::with_capacity(configured.bindings.len()); + for native_index in 0..configured.bindings.len() { + let binding = + configured.bindings.get(&native_index).ok_or(PortNotBound { native_index })?; + bindings_vec.push(*binding); + } + let bound_proto_interface: Vec<(_, _)> = (0..num_bindings) + .map(|i| configured.bindings.get()) .proto_maybe_bindings .iter() .copied() diff --git a/src/runtime/errors.rs b/src/runtime/errors.rs index e3fff2963004eca66c6090b0289be109a4315383..474d535f152ef2bd8af1b0ad974a06d4071df254 100644 --- a/src/runtime/errors.rs +++ b/src/runtime/errors.rs @@ -4,7 +4,6 @@ use crate::common::*; pub enum PortBindErr { AlreadyConnected, IndexOutOfBounds, - PortAlreadyBound, NotConfigured, ParseErr, AlreadyConfigured, diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 8c3629e2eb715bc1ab2241594ec80afbebe396b2..ba3908e2bf87d684844ef0044ec1374b6eec2f34 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -49,18 +49,17 @@ pub struct Unconfigured { pub controller_id: ControllerId, } #[derive(Debug)] +pub struct Configured { + controller_id: ControllerId, + bindings: HashMap, + protocol_description: Arc, +} +#[derive(Debug)] pub struct Connected { native_interface: Vec<(Key, Polarity)>, sync_batches: Vec, controller: Controller, } -#[derive(Debug)] -pub struct Configured { - // invariant: proto_maybe_bindings.len() is the size of the protocol's interface - controller_id: ControllerId, - proto_maybe_bindings: Vec<(Polarity, Option)>, - protocol_description: Arc, -} #[derive(Debug, Copy, Clone)] pub enum PortBinding {