diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-04-04 16:30:23 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-04-04 16:30:23 +0200 |
commit | 412d3e81d757038bd5d8bb15e4f4ceba9f5acd6e (patch) | |
tree | ef7964d1e77aa70ecaadd59aeca4c75f21c1ea94 /src/test_impl.rs | |
parent | 51d82ced3564aa2f4e49aab6fdeb814652de084a (diff) |
Remove Node::id() requirement
A `Node` instance should not need to know about its id, because this
would restrict us in case of IPFS, where we know the ID only _after_
adding the data to the storage.
This commit thus removes the requirement `Node::id()`.
The interface `DagBackend::get()` now returns the `NodeId` for the
`Node` as well. This is mostly because it makes the implementation of
`AsyncDag` less complicated.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Tested-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/test_impl.rs')
-rw-r--r-- | src/test_impl.rs | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/src/test_impl.rs b/src/test_impl.rs index cd6dd26..672c3fc 100644 --- a/src/test_impl.rs +++ b/src/test_impl.rs @@ -17,18 +17,14 @@ impl crate::NodeId for Id {} #[derive(Clone, Debug)] pub struct Node { - pub(crate) id: Id, pub(crate) parents: Vec<Id>, - pub(crate) data: u8, + // data the node holds, used to create the ID in tests as "hashing" for unique id + pub(crate) data: usize, } impl crate::Node for Node { type Id = Id; - fn id(&self) -> &Self::Id { - &self.id - } - fn parent_ids(&self) -> Vec<Self::Id> { self.parents.clone() } @@ -50,20 +46,20 @@ impl Backend { #[async_trait] impl crate::DagBackend<Id, Node> for Backend { - async fn get(&self, id: Id) -> Result<Option<Node>> { + async fn get(&self, id: Id) -> Result<Option<(Id, Node)>> { if self.0.read().unwrap().len() < id.0 + 1 { Ok(None) } else { - Ok(self.0.read().unwrap()[id.0].clone()) + Ok(self.0.read().unwrap()[id.0].clone().map(|node| (id, node))) } } async fn put(&mut self, node: Node) -> Result<Id> { - while self.0.read().unwrap().len() < node.id.0 + 1 { + while self.0.read().unwrap().len() < node.data + 1 { self.0.write().unwrap().push(None) } - let idx = node.id.0; + let idx = node.data; self.0.write().unwrap()[idx] = Some(node); Ok(Id(idx)) } |