Skip to content

Commit

Permalink
introduce WeakSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
hellow554 committed Dec 3, 2018
1 parent a46c556 commit 34a4c84
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/port/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ mod pport;

pub(crate) use self::portconnector::PortConnector;

use std::sync::Weak;

use crate::Signal;
use crate::signal::WeakSignal;

pub use self::portdirection::{Dir, InOut, Input, MaybeRead, MaybeWrite, Off, Output, PortDirection, Read, Write};
pub use self::pport::Port;

#[derive(Debug)]
pub(crate) struct InnerPort<T> {
value: RwLock<T>,
signal: Weak<Signal<T>>,
signal: WeakSignal<T>,
}
6 changes: 3 additions & 3 deletions src/port/pport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use super::InnerPort;
use crate::direction::{Dir, InOut, Input, MaybeRead, MaybeWrite, Output, PortDirection, Read, Write};
use crate::dump::IterValues;
use crate::port::portconnector::PortConnector;
use crate::signal::WeakSignal;
use crate::Ieee1164;
use std::sync::Weak;

#[allow(unused)]
use crate::{models::gates::AndGate, Signal};
Expand Down Expand Up @@ -70,7 +70,7 @@ impl<T, D: PortDirection> Port<T, D> {
Port {
inner: Arc::new(InnerPort {
value: RwLock::new(value),
signal: Weak::new(),
signal: WeakSignal::default(),
}),
_marker: PhantomData,
}
Expand Down Expand Up @@ -110,7 +110,7 @@ where
/// ```
// FIXME!
pub fn is_connected(&self) -> bool {
self.inner.signal.upgrade().is_some()
self.inner.signal.is_strong()
}
}

Expand Down
26 changes: 20 additions & 6 deletions src/signal.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
use std::sync::{Arc, RwLock};
use std::convert::TryInto;
use std::sync::{Arc, RwLock, Weak};

use crate::direction::{Input, Output, PortDirection};
use crate::logicbit::Resolve;

use crate::direction::{Input, Output};

use crate::port::PortConnector;
use crate::port::PortDirection;
use crate::{Port, Updateable};
use std::convert::TryInto;

#[derive(Debug, Clone)]
pub struct Signal<T> {
inner: Arc<InnerSignal<T>>,
}

#[derive(Debug, Clone)]
pub(crate) struct WeakSignal<T> {
inner: Weak<InnerSignal<T>>,
}

impl<T> Default for WeakSignal<T> {
fn default() -> Self {
WeakSignal { inner: Weak::new() }
}
}

impl<T> WeakSignal<T> {
pub(crate) fn is_strong(&self) -> bool {
self.inner.upgrade().is_some()
}
}

#[derive(Debug)]
struct InnerSignal<T> {
input_ports: RwLock<Vec<PortConnector<T, Input>>>,
Expand Down

0 comments on commit 34a4c84

Please sign in to comment.