forked from M-Labs/nac3
1
0
Fork 0

rpc: strings

This commit is contained in:
Sebastien Bourdeauducq 2020-06-01 18:21:04 +08:00
parent 7aa9a95c21
commit cb24b82e68
2 changed files with 14 additions and 5 deletions

View File

@ -123,3 +123,14 @@ pub async fn write_i64(stream: &TcpStream, value: i64) -> Result<()> {
value as u8].iter().copied()).await?; value as u8].iter().copied()).await?;
Ok(()) Ok(())
} }
pub async fn write_bytes(stream: &TcpStream, value: &[u8]) -> Result<()> {
write_i32(stream, value.len() as i32).await?;
stream.send(value.iter().copied()).await?;
Ok(())
}
pub async fn write_string(stream: &TcpStream, value: &str) -> Result<()> {
write_bytes(stream, value.as_bytes()).await?;
Ok(())
}

View File

@ -119,10 +119,8 @@ async unsafe fn send_value(stream: &TcpStream, tag: Tag<'_>, data: &mut *const (
consume_value!(i64, |ptr| consume_value!(i64, |ptr|
write_i64(stream, *ptr).await), write_i64(stream, *ptr).await),
Tag::String => Tag::String =>
//consume_value!(CSlice<u8>, |ptr| consume_value!(CSlice<u8>, |ptr|
// writer.write_string(str::from_utf8((*ptr).as_ref()).unwrap())), write_string(stream, str::from_utf8((*ptr).as_ref()).unwrap()).await),
// TODO
unimplemented!(),
Tag::Bytes | Tag::ByteArray => Tag::Bytes | Tag::ByteArray =>
consume_value!(CSlice<u8>, |ptr| consume_value!(CSlice<u8>, |ptr|
stream.send((*ptr).as_ref().iter().copied()).await), stream.send((*ptr).as_ref().iter().copied()).await),
@ -157,7 +155,7 @@ async unsafe fn send_value(stream: &TcpStream, tag: Tag<'_>, data: &mut *const (
Tag::Keyword(it) => { Tag::Keyword(it) => {
struct Keyword<'a> { name: CSlice<'a, u8> }; struct Keyword<'a> { name: CSlice<'a, u8> };
consume_value!(Keyword, |ptr| { consume_value!(Keyword, |ptr| {
//TODO writer.write_string(str::from_utf8((*ptr).name.as_ref()).unwrap())?; write_string(stream, str::from_utf8((*ptr).name.as_ref()).unwrap()).await?;
let tag = it.clone().next().expect("truncated tag"); let tag = it.clone().next().expect("truncated tag");
let mut data = ptr.offset(1) as *const (); let mut data = ptr.offset(1) as *const ();
// TODO send_value(stream, tag, &mut data).await // TODO send_value(stream, tag, &mut data).await