This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from DyrellC/v2.0/test-topics
[v2.0] Introduce Topics
- Loading branch information
Showing
18 changed files
with
776 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use alloc::{ | ||
string::{String, ToString}, | ||
vec::Vec, | ||
}; | ||
use core::{ | ||
convert::{TryFrom, TryInto}, | ||
fmt::Formatter, | ||
}; | ||
use spongos::{ | ||
ddml::{ | ||
commands::{sizeof, unwrap, wrap, Mask}, | ||
io, | ||
types::Bytes, | ||
}, | ||
PRP, | ||
}; | ||
|
||
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash)] | ||
pub struct Topic(String); | ||
|
||
impl Topic { | ||
pub fn new(t: String) -> Self { | ||
Self(t) | ||
} | ||
} | ||
|
||
impl From<&str> for Topic { | ||
fn from(t: &str) -> Self { | ||
Self(t.to_string()) | ||
} | ||
} | ||
|
||
impl From<String> for Topic { | ||
fn from(t: String) -> Self { | ||
Self(t) | ||
} | ||
} | ||
|
||
impl TryFrom<&[u8]> for Topic { | ||
type Error = anyhow::Error; | ||
fn try_from(t: &[u8]) -> Result<Self, Self::Error> { | ||
let topic = String::from_utf8(t.to_vec())?; | ||
Ok(Topic(topic)) | ||
} | ||
} | ||
|
||
impl TryFrom<Vec<u8>> for Topic { | ||
type Error = anyhow::Error; | ||
fn try_from(t: Vec<u8>) -> Result<Self, Self::Error> { | ||
let topic = String::from_utf8(t)?; | ||
Ok(Topic(topic)) | ||
} | ||
} | ||
|
||
impl core::fmt::Display for Topic { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { | ||
write!(f, "{}", &self.0) | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Topic { | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl Mask<&Topic> for sizeof::Context { | ||
fn mask(&mut self, topic: &Topic) -> anyhow::Result<&mut Self> { | ||
self.mask(Bytes::new(topic)) | ||
} | ||
} | ||
|
||
impl<OS, F> Mask<&Topic> for wrap::Context<OS, F> | ||
where | ||
F: PRP, | ||
OS: io::OStream, | ||
{ | ||
fn mask(&mut self, topic: &Topic) -> anyhow::Result<&mut Self> { | ||
self.mask(Bytes::new(topic)) | ||
} | ||
} | ||
|
||
impl<IS, F> Mask<&mut Topic> for unwrap::Context<IS, F> | ||
where | ||
F: PRP, | ||
IS: io::IStream, | ||
{ | ||
fn mask(&mut self, topic: &mut Topic) -> anyhow::Result<&mut Self> { | ||
let mut topic_bytes = topic.as_ref().to_vec(); | ||
self.mask(Bytes::new(&mut topic_bytes))?; | ||
*topic = topic_bytes.try_into()?; | ||
Ok(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.