Skip to content

Commit 2f0145e

Browse files
committed
Fix Response returning dg vec instead of DataGroup
Also implemented `IntoIterator` for `DataGroup`
1 parent 00f428a commit 2f0145e

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

src/connection.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
//! # Database connections
19-
//!
19+
//!
2020
//! This crate provides a [`Connection`] object that can be used to connect to a Skytable database instance
2121
//! and write/read queries/responses to/from it
2222
@@ -25,7 +25,7 @@ use crate::{Query, Response};
2525
use bytes::{Buf, BytesMut};
2626
pub use std::io::Result as IoResult;
2727
use std::io::{Error, ErrorKind};
28-
use tokio::io::{AsyncReadExt};
28+
use tokio::io::AsyncReadExt;
2929
use tokio::net::TcpStream;
3030

3131
/// 4 KB Read Buffer
@@ -68,13 +68,9 @@ impl Connection {
6868
ClientResult::Incomplete => {
6969
continue;
7070
}
71-
ClientResult::Response(r, f) => {
71+
ClientResult::SimpleResponse(r, f) => {
7272
self.buffer.advance(f);
73-
if r.len() != 1 {
74-
break Ok(Response::InvalidResponse);
75-
} else {
76-
break Ok(Response::Array(r));
77-
}
73+
break Ok(Response::Array(r));
7874
}
7975
ClientResult::ResponseItem(r, f) => {
8076
self.buffer.advance(f);
@@ -88,6 +84,9 @@ impl Connection {
8884
self.buffer.clear();
8985
break Ok(Response::ParseError);
9086
}
87+
ClientResult::PipelinedResponse(_, _) => {
88+
todo!("Pipelined queries haven't been implemented yet!")
89+
}
9190
}
9291
}
9392
}

src/deserializer.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! This module provides methods to deserialize an incoming response packet
1919
2020
use crate::terrapipe::RespCode;
21-
use std::hint::unreachable_unchecked;
21+
use std::vec;
2222

2323
#[derive(Debug, PartialEq)]
2424
/// A response datagroup
@@ -31,8 +31,13 @@ impl DataGroup {
3131
fn size(&self) -> usize {
3232
self.0.len()
3333
}
34-
fn __unpack_0(mut self) -> DataType {
35-
self.0.pop().unwrap()
34+
}
35+
36+
impl IntoIterator for DataGroup {
37+
type Item = DataType;
38+
type IntoIter = vec::IntoIter<Self::Item>;
39+
fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
40+
self.0.into_iter()
3641
}
3742
}
3843

@@ -70,7 +75,9 @@ pub enum ClientResult {
7075
/// The response was Invalid
7176
InvalidResponse,
7277
/// The response is a valid response and has been parsed into a vector of datagroups
73-
Response(Vec<DataGroup>, usize),
78+
PipelinedResponse(Vec<DataGroup>, usize),
79+
/// The response is a valid response and has been parsed into a datagroup
80+
SimpleResponse(DataGroup, usize),
7481
/// A single element in a datagroup (please note that this is a client abstraction)
7582
ResponseItem(DataType, usize),
7683
/// The response was empty, which means that the remote end closed the connection
@@ -237,19 +244,13 @@ pub fn parse(buf: &[u8]) -> ClientResult {
237244
if items.len() == 1 {
238245
if items[0].size() == 1 {
239246
// Single item returned, so we can return this as ClientResult::ResponseItem
240-
ClientResult::ResponseItem(
241-
items
242-
.pop()
243-
.unwrap_or_else(|| unsafe { unreachable_unchecked() })
244-
.__unpack_0(),
245-
pos,
246-
)
247+
ClientResult::ResponseItem(items.swap_remove(0).0.swap_remove(0), pos)
247248
} else {
248249
// More than one time returned, so we can return this as ClientResult::Response
249-
ClientResult::Response(items, pos)
250+
ClientResult::SimpleResponse(items.swap_remove(0), pos)
250251
}
251252
} else {
252-
todo!("Pipelined queries aren't implemented yet!")
253+
ClientResult::PipelinedResponse(items, pos)
253254
}
254255
} else {
255256
// Since the number of items we got is not equal to the action size - not all data was
@@ -317,14 +318,14 @@ fn test_parser() {
317318
.to_owned();
318319
assert_eq!(
319320
parse(&res),
320-
ClientResult::Response(
321-
vec![DataGroup(vec![
321+
ClientResult::SimpleResponse(
322+
DataGroup(vec![
322323
DataType::RespCode(RespCode::NotFound),
323324
DataType::RespCode(RespCode::Okay),
324325
DataType::Str("sayan".to_owned()),
325326
DataType::Str("is".to_owned()),
326327
DataType::Str("busy".to_owned())
327-
])],
328+
]),
328329
res.len()
329330
)
330331
);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub enum Response {
186186
/// The server sent an invalid response
187187
InvalidResponse,
188188
/// An array of items
189-
Array(Vec<DataGroup>),
189+
Array(DataGroup),
190190
/// A single item
191191
///
192192
/// This is a client abstraction for a datagroup that only has one element

0 commit comments

Comments
 (0)