async_serial.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use futures::prelude::*;
  2. use crate::endian;
  3. use crate::error::{Error, Result};
  4. use crate::serial::VarInt;
  5. impl VarInt {
  6. pub async fn encode_async<W: AsyncWrite + Unpin>(&self, stream: &mut W) -> Result<usize> {
  7. match self.0 {
  8. 0..=0xFC => {
  9. AsyncWriteExt::write_u8(stream, self.0 as u8).await?;
  10. Ok(1)
  11. }
  12. 0xFD..=0xFFFF => {
  13. AsyncWriteExt::write_u8(stream, 0xFD).await?;
  14. AsyncWriteExt::write_u16(stream, self.0 as u16).await?;
  15. Ok(3)
  16. }
  17. 0x10000..=0xFFFFFFFF => {
  18. AsyncWriteExt::write_u8(stream, 0xFE).await?;
  19. AsyncWriteExt::write_u32(stream, self.0 as u32).await?;
  20. Ok(5)
  21. }
  22. _ => {
  23. AsyncWriteExt::write_u8(stream, 0xFF).await?;
  24. AsyncWriteExt::write_u64(stream, self.0 as u64).await?;
  25. Ok(9)
  26. }
  27. }
  28. }
  29. pub async fn decode_async<R: AsyncRead + Unpin>(stream: &mut R) -> Result<Self> {
  30. let n = AsyncReadExt::read_u8(stream).await?;
  31. match n {
  32. 0xFF => {
  33. let x = AsyncReadExt::read_u64(stream).await?;
  34. if x < 0x100000000 {
  35. Err(Error::NonMinimalVarInt)
  36. } else {
  37. Ok(VarInt(x))
  38. }
  39. }
  40. 0xFE => {
  41. let x = AsyncReadExt::read_u32(stream).await?;
  42. if x < 0x10000 {
  43. Err(Error::NonMinimalVarInt)
  44. } else {
  45. Ok(VarInt(x as u64))
  46. }
  47. }
  48. 0xFD => {
  49. let x = AsyncReadExt::read_u16(stream).await?;
  50. if x < 0xFD {
  51. Err(Error::NonMinimalVarInt)
  52. } else {
  53. Ok(VarInt(x as u64))
  54. }
  55. }
  56. n => Ok(VarInt(n as u64)),
  57. }
  58. }
  59. }
  60. macro_rules! async_encoder_fn {
  61. ($name:ident, $val_type:ty, $writefn:ident) => {
  62. #[inline]
  63. pub async fn $name<W: AsyncWrite + Unpin>(stream: &mut W, v: $val_type) -> Result<()> {
  64. stream
  65. .write_all(&endian::$writefn(v))
  66. .await
  67. .map_err(Error::Io)
  68. }
  69. };
  70. }
  71. macro_rules! async_decoder_fn {
  72. ($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => {
  73. pub async fn $name<R: AsyncRead + Unpin>(stream: &mut R) -> Result<$val_type> {
  74. assert_eq!(::std::mem::size_of::<$val_type>(), $byte_len); // size_of isn't a constfn in 1.22
  75. let mut val = [0; $byte_len];
  76. stream.read_exact(&mut val[..]).await.map_err(Error::Io)?;
  77. Ok(endian::$readfn(&val))
  78. }
  79. };
  80. }
  81. pub struct AsyncReadExt {}
  82. impl AsyncReadExt {
  83. async_decoder_fn!(read_u64, u64, slice_to_u64_le, 8);
  84. async_decoder_fn!(read_u32, u32, slice_to_u32_le, 4);
  85. async_decoder_fn!(read_u16, u16, slice_to_u16_le, 2);
  86. pub async fn read_u8<R: AsyncRead + Unpin>(stream: &mut R) -> Result<u8> {
  87. let mut slice = [0u8; 1];
  88. stream.read_exact(&mut slice).await?;
  89. Ok(slice[0])
  90. }
  91. }
  92. pub struct AsyncWriteExt {}
  93. impl AsyncWriteExt {
  94. async_encoder_fn!(write_u64, u64, u64_to_array_le);
  95. async_encoder_fn!(write_u32, u32, u32_to_array_le);
  96. async_encoder_fn!(write_u16, u16, u16_to_array_le);
  97. pub async fn write_u8<W: AsyncWrite + Unpin>(stream: &mut W, v: u8) -> Result<()> {
  98. stream.write_all(&[v]).await.map_err(Error::Io)
  99. }
  100. }