async_serial.rs 3.5 KB

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