Skip to content

Commit ec5326e

Browse files
committed
refactor: rename sqlx_core::io::{Encode, Decode} for clarity
1 parent 2cb6217 commit ec5326e

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

sqlx-core/src/io/decode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ use bytes::Bytes;
22

33
use crate::error::Error;
44

5-
pub trait Decode<'de, Context = ()>
5+
pub trait ProtocolDecode<'de, Context = ()>
66
where
77
Self: Sized,
88
{
99
fn decode(buf: Bytes) -> Result<Self, Error>
1010
where
11-
Self: Decode<'de, ()>,
11+
Self: ProtocolDecode<'de, ()>,
1212
{
1313
Self::decode_with(buf, ())
1414
}
1515

1616
fn decode_with(buf: Bytes, context: Context) -> Result<Self, Error>;
1717
}
1818

19-
impl Decode<'_> for Bytes {
19+
impl ProtocolDecode<'_> for Bytes {
2020
fn decode_with(buf: Bytes, _: ()) -> Result<Self, Error> {
2121
Ok(buf)
2222
}
2323
}
2424

25-
impl Decode<'_> for () {
25+
impl ProtocolDecode<'_> for () {
2626
fn decode_with(_: Bytes, _: ()) -> Result<(), Error> {
2727
Ok(())
2828
}

sqlx-core/src/io/encode.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
pub trait Encode<'en, Context = ()> {
2-
fn encode(&self, buf: &mut Vec<u8>)
1+
pub trait ProtocolEncode<'en, Context = ()> {
2+
fn encode(&self, buf: &mut Vec<u8>) -> Result<(), crate::Error>
33
where
4-
Self: Encode<'en, ()>,
4+
Self: ProtocolEncode<'en, ()>,
55
{
6-
self.encode_with(buf, ());
6+
self.encode_with(buf, ())
77
}
88

9-
fn encode_with(&self, buf: &mut Vec<u8>, context: Context);
9+
fn encode_with(&self, buf: &mut Vec<u8>, context: Context) -> Result<(), crate::Error>;
1010
}
1111

12-
impl<'en, C> Encode<'en, C> for &'_ [u8] {
13-
fn encode_with(&self, buf: &mut Vec<u8>, _: C) {
12+
impl<'en, C> ProtocolEncode<'en, C> for &'_ [u8] {
13+
fn encode_with(&self, buf: &mut Vec<u8>, _context: C) -> Result<(), crate::Error> {
1414
buf.extend_from_slice(self);
15+
Ok(())
1516
}
1617
}

sqlx-core/src/io/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ mod read_buf;
99
pub use buf::BufExt;
1010
pub use buf_mut::BufMutExt;
1111
//pub use buf_stream::BufStream;
12-
pub use decode::Decode;
13-
pub use encode::Encode;
12+
pub use decode::ProtocolDecode;
13+
pub use encode::ProtocolEncode;
1414
pub use read_buf::ReadBuf;
1515

1616
#[cfg(not(feature = "_rt-tokio"))]

sqlx-core/src/net/socket/buffered.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{cmp, io};
44

55
use crate::error::Error;
66

7-
use crate::io::{AsyncRead, AsyncReadExt, Decode, Encode};
7+
use crate::io::{AsyncRead, AsyncReadExt, ProtocolDecode, ProtocolEncode};
88

99
// Tokio, async-std, and std all use this as the default capacity for their buffered I/O.
1010
const DEFAULT_BUF_SIZE: usize = 8192;
@@ -59,32 +59,36 @@ impl<S: Socket> BufferedSocket<S> {
5959

6060
pub async fn read<'de, T>(&mut self, byte_len: usize) -> Result<T, Error>
6161
where
62-
T: Decode<'de, ()>,
62+
T: ProtocolDecode<'de, ()>,
6363
{
6464
self.read_with(byte_len, ()).await
6565
}
6666

6767
pub async fn read_with<'de, T, C>(&mut self, byte_len: usize, context: C) -> Result<T, Error>
6868
where
69-
T: Decode<'de, C>,
69+
T: ProtocolDecode<'de, C>,
7070
{
7171
T::decode_with(self.read_buffered(byte_len).await?.freeze(), context)
7272
}
7373

74-
pub fn write<'en, T>(&mut self, value: T)
74+
#[inline(always)]
75+
pub fn write<'en, T>(&mut self, value: T) -> Result<(), Error>
7576
where
76-
T: Encode<'en, ()>,
77+
T: ProtocolEncode<'en, ()>,
7778
{
7879
self.write_with(value, ())
7980
}
8081

81-
pub fn write_with<'en, T, C>(&mut self, value: T, context: C)
82+
#[inline(always)]
83+
pub fn write_with<'en, T, C>(&mut self, value: T, context: C) -> Result<(), Error>
8284
where
83-
T: Encode<'en, C>,
85+
T: ProtocolEncode<'en, C>,
8486
{
85-
value.encode_with(self.write_buf.buf_mut(), context);
87+
value.encode_with(self.write_buf.buf_mut(), context)?;
8688
self.write_buf.bytes_written = self.write_buf.buf.len();
8789
self.write_buf.sanity_check();
90+
91+
Ok(())
8892
}
8993

9094
pub async fn flush(&mut self) -> io::Result<()> {

0 commit comments

Comments
 (0)