runs cargofmt (#3)

This commit is contained in:
Avery Harnish
2020-02-11 15:27:49 -06:00
committed by GitHub
parent 358e95e57c
commit 9852de9924
11 changed files with 31 additions and 32 deletions

View File

@@ -1,4 +1,3 @@
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
unimplemented!(); unimplemented!();

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@

View File

@@ -2,8 +2,8 @@
use crate::Connection; use crate::Connection;
use bytes::Bytes; use bytes::Bytes;
use tokio::net::{TcpStream, ToSocketAddrs};
use std::io; use std::io;
use tokio::net::{TcpStream, ToSocketAddrs};
/// Mini asynchronous Redis client /// Mini asynchronous Redis client
pub struct Client { pub struct Client {

View File

@@ -1,5 +1,5 @@
use crate::{Connection, Frame, Kv};
use crate::cmd::{Parse, ParseError}; use crate::cmd::{Parse, ParseError};
use crate::{Connection, Frame, Kv};
use bytes::Bytes; use bytes::Bytes;
use std::io; use std::io;

View File

@@ -1,10 +1,10 @@
use crate::{Command, Connection, Frame, Kv, Shutdown};
use crate::cmd::{Parse, ParseError}; use crate::cmd::{Parse, ParseError};
use crate::{Command, Connection, Frame, Kv, Shutdown};
use bytes::Bytes; use bytes::Bytes;
use tokio::select;
use tokio::stream::{StreamMap, StreamExt};
use std::io; use std::io;
use tokio::select;
use tokio::stream::{StreamExt, StreamMap};
#[derive(Debug)] #[derive(Debug)]
pub struct Subscribe { pub struct Subscribe {
@@ -49,7 +49,6 @@ impl Subscribe {
dst: &mut Connection, dst: &mut Connection,
shutdown: &mut Shutdown, shutdown: &mut Shutdown,
) -> io::Result<()> { ) -> io::Result<()> {
// Each individual channel subscription is handled using a // Each individual channel subscription is handled using a
// `sync::broadcast` channel. Messages are then fanned out to all // `sync::broadcast` channel. Messages are then fanned out to all
// clients currently subscribed to the channels. // clients currently subscribed to the channels.

View File

@@ -1,9 +1,9 @@
use crate::frame::{self, Frame}; use crate::frame::{self, Frame};
use bytes::{Buf, BytesMut}; use bytes::{Buf, BytesMut};
use tokio::io::{BufStream, AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use std::io::{self, Cursor}; use std::io::{self, Cursor};
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufStream};
use tokio::net::TcpStream;
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Connection { pub(crate) struct Connection {

View File

@@ -74,7 +74,7 @@ impl Frame {
b'*' => { b'*' => {
let len = get_decimal(src)?; let len = get_decimal(src)?;
for _ in 0.. len { for _ in 0..len {
Frame::check(src)?; Frame::check(src)?;
} }
@@ -139,7 +139,7 @@ impl Frame {
let len = get_decimal(src)?.try_into()?; let len = get_decimal(src)?.try_into()?;
let mut out = Vec::with_capacity(len); let mut out = Vec::with_capacity(len);
for _ in 0.. len { for _ in 0..len {
out.push(Box::new(Frame::parse(src)?)); out.push(Box::new(Frame::parse(src)?));
} }
@@ -181,8 +181,7 @@ fn get_decimal(src: &mut Cursor<&[u8]>) -> Result<u64, Error> {
let line = get_line(src)?; let line = get_line(src)?;
atoi::<u64>(line) atoi::<u64>(line).ok_or(Error::Invalid)
.ok_or(Error::Invalid)
} }
/// Find a line /// Find a line
@@ -193,7 +192,7 @@ fn get_line<'a>(src: &mut Cursor<&'a [u8]>) -> Result<&'a [u8], Error> {
let end = src.get_ref().len() - 1; let end = src.get_ref().len() - 1;
for i in start..end { for i in start..end {
if src.get_ref()[i] == b'\r' && src.get_ref()[i+1] == b'\n' { if src.get_ref()[i] == b'\r' && src.get_ref()[i + 1] == b'\n' {
// We found a line, update the position to be *after* the \n // We found a line, update the position to be *after* the \n
src.set_position((i + 2) as u64); src.set_position((i + 2) as u64);

View File

@@ -1,8 +1,8 @@
use bytes::Bytes; use bytes::Bytes;
use tokio::sync::broadcast;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use tokio::sync::broadcast;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct Kv { pub(crate) struct Kv {
@@ -44,9 +44,7 @@ impl Kv {
let mut shared = self.shared.lock().unwrap(); let mut shared = self.shared.lock().unwrap();
match shared.pub_sub.entry(key) { match shared.pub_sub.entry(key) {
Entry::Occupied(e) => { Entry::Occupied(e) => e.get().subscribe(),
e.get().subscribe()
}
Entry::Vacant(e) => { Entry::Vacant(e) => {
let (tx, rx) = broadcast::channel(1028); let (tx, rx) = broadcast::channel(1028);
e.insert(tx); e.insert(tx);
@@ -58,7 +56,9 @@ impl Kv {
pub(crate) fn publish(&self, key: &str, value: Bytes) -> usize { pub(crate) fn publish(&self, key: &str, value: Bytes) -> usize {
let shared = self.shared.lock().unwrap(); let shared = self.shared.lock().unwrap();
shared.pub_sub.get(key) shared
.pub_sub
.get(key)
.map(|tx| tx.send(value).unwrap_or(0)) .map(|tx| tx.send(value).unwrap_or(0))
.unwrap_or(0) .unwrap_or(0)
} }

View File

@@ -23,11 +23,14 @@ impl Parse {
_ => return Err(ParseError::Invalid), _ => return Err(ParseError::Invalid),
}; };
Ok(Parse { parts: array.into_iter() }) Ok(Parse {
parts: array.into_iter(),
})
} }
fn next(&mut self) -> Result<Frame, ParseError> { fn next(&mut self) -> Result<Frame, ParseError> {
self.parts.next() self.parts
.next()
.map(|frame| *frame) .map(|frame| *frame)
.ok_or(ParseError::EndOfStream) .ok_or(ParseError::EndOfStream)
} }
@@ -35,11 +38,9 @@ impl Parse {
pub(crate) fn next_string(&mut self) -> Result<String, ParseError> { pub(crate) fn next_string(&mut self) -> Result<String, ParseError> {
match self.next()? { match self.next()? {
Frame::Simple(s) => Ok(s), Frame::Simple(s) => Ok(s),
Frame::Bulk(data) => { Frame::Bulk(data) => str::from_utf8(&data[..])
str::from_utf8(&data[..]) .map(|s| s.to_string())
.map(|s| s.to_string()) .map_err(|_| ParseError::Invalid),
.map_err(|_| ParseError::Invalid)
}
_ => Err(ParseError::Invalid), _ => Err(ParseError::Invalid),
} }
} }
@@ -79,6 +80,7 @@ impl From<ParseError> for io::Error {
EndOfStream => "end of stream".to_string(), EndOfStream => "end of stream".to_string(),
Invalid => "invalid".to_string(), Invalid => "invalid".to_string(),
UnknownCommand(cmd) => format!("unknown command `{}`", cmd), UnknownCommand(cmd) => format!("unknown command `{}`", cmd),
}) },
)
} }
} }

View File

@@ -1,4 +1,4 @@
use crate::{Connection, Command, Kv, Shutdown}; use crate::{Command, Connection, Kv, Shutdown};
use tokio::io; use tokio::io;
use tokio::net::TcpListener; use tokio::net::TcpListener;
@@ -90,10 +90,8 @@ impl Handler {
let cmd = Command::from_frame(frame)?; let cmd = Command::from_frame(frame)?;
cmd.apply( cmd.apply(&self.kv, &mut self.connection, &mut self.shutdown)
&self.kv, .await?;
&mut self.connection,
&mut self.shutdown).await?;
} }
Ok(()) Ok(())