-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to Rust 2021, and auto-apply clippy lints #148
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ use std::borrow::Cow; | |
pub struct Options { | ||
pub noreply: bool, | ||
pub exptime: u32, | ||
pub flags: u32, | ||
pub cas: Option<u64>, | ||
} | ||
|
||
|
@@ -27,7 +26,7 @@ enum StoreCommand { | |
Prepend, | ||
} | ||
|
||
const END: &'static str = "END\r\n"; | ||
const END: &str = "END\r\n"; | ||
|
||
impl fmt::Display for StoreCommand { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
|
@@ -45,15 +44,13 @@ impl fmt::Display for StoreCommand { | |
struct CappedLineReader<C> { | ||
inner: C, | ||
filled: usize, | ||
buf: [u8; 2048], | ||
buf: Vec<u8>, | ||
} | ||
|
||
fn get_line(buf: &[u8]) -> Option<usize> { | ||
for (i, r) in buf.iter().enumerate() { | ||
if *r == b'\r' { | ||
if buf.get(i + 1) == Some(&b'\n') { | ||
return Some(i + 2); | ||
} | ||
if *r == b'\r' && buf.get(i + 1) == Some(&b'\n') { | ||
return Some(i + 2); | ||
} | ||
} | ||
None | ||
|
@@ -64,7 +61,7 @@ impl<C: Read> CappedLineReader<C> { | |
Self { | ||
inner, | ||
filled: 0, | ||
buf: [0x0; 2048], | ||
buf: vec![0x0; 2048], | ||
} | ||
} | ||
|
||
|
@@ -77,7 +74,7 @@ impl<C: Read> CappedLineReader<C> { | |
let (to_fill, rest) = buf.split_at_mut(min); | ||
to_fill.copy_from_slice(&self.buf[..min]); | ||
self.consume(min); | ||
if rest.len() != 0 { | ||
if !rest.is_empty() { | ||
self.inner.read_exact(&mut rest[..])?; | ||
} | ||
Ok(()) | ||
|
@@ -98,7 +95,7 @@ impl<C: Read> CappedLineReader<C> { | |
} | ||
loop { | ||
let (_filled, buf) = self.buf.split_at_mut(self.filled); | ||
if buf.len() == 0 { | ||
if buf.is_empty() { | ||
return Err(ClientError::Error(Cow::Borrowed("Ascii protocol response too long")))?; | ||
} | ||
let read = self.inner.read(&mut buf[..])?; | ||
|
@@ -131,7 +128,7 @@ impl ProtocolTrait for AsciiProtocol<Stream> { | |
} | ||
|
||
fn version(&mut self) -> Result<String, MemcacheError> { | ||
self.reader.get_mut().write(b"version\r\n")?; | ||
self.reader.get_mut().write_all(b"version\r\n")?; | ||
self.reader.get_mut().flush()?; | ||
self.reader.read_line(|response| { | ||
let response = MemcacheError::try_from(response)?; | ||
|
@@ -294,7 +291,7 @@ impl ProtocolTrait for AsciiProtocol<Stream> { | |
} | ||
|
||
fn stats(&mut self) -> Result<Stats, MemcacheError> { | ||
self.reader.get_mut().write(b"stats\r\n")?; | ||
self.reader.get_mut().write_all(b"stats\r\n")?; | ||
self.reader.get_mut().flush()?; | ||
|
||
enum Loop { | ||
|
@@ -312,7 +309,7 @@ impl ProtocolTrait for AsciiProtocol<Stream> { | |
if !s.starts_with("STAT") { | ||
return Err(ServerError::BadResponse(Cow::Owned(s.into())))?; | ||
} | ||
let stat: Vec<_> = s.trim_end_matches("\r\n").split(" ").collect(); | ||
let stat: Vec<_> = s.trim_end_matches("\r\n").split(' ').collect(); | ||
if stat.len() < 3 { | ||
return Err(ServerError::BadResponse(Cow::Owned(s.into())).into()); | ||
} | ||
|
@@ -348,12 +345,10 @@ impl AsciiProtocol<Stream> { | |
value: V, | ||
options: &Options, | ||
) -> Result<bool, MemcacheError> { | ||
if command == StoreCommand::Cas { | ||
if options.cas.is_none() { | ||
Err(ClientError::Error(Cow::Borrowed( | ||
"cas_id should be present when using cas command", | ||
)))?; | ||
} | ||
if command == StoreCommand::Cas && options.cas.is_none() { | ||
Err(ClientError::Error(Cow::Borrowed( | ||
"cas_id should be present when using cas command", | ||
)))?; | ||
} | ||
let noreply = if options.noreply { " noreply" } else { "" }; | ||
if options.cas.is_some() { | ||
Comment on lines
345
to
354
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The verification process did not find any test cases specifically covering the Analysis chain
The changes in the Scripts ExecutedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Verify comprehensive testing of the `store` method, particularly for edge cases involving CAS operations.
rg --type rust "store.*test"
Length of output: 30 Script: #!/bin/bash
# Broaden the search to find any test cases related to the `store` method or CAS operations in the test files.
rg --type rust --files-with-matches 'fn test' | xargs rg 'store|CAS'
Length of output: 703 |
||
|
@@ -382,7 +377,7 @@ impl AsciiProtocol<Stream> { | |
} | ||
|
||
value.write_to(self.reader.get_mut())?; | ||
self.reader.get_mut().write(b"\r\n")?; | ||
self.reader.get_mut().write_all(b"\r\n")?; | ||
self.reader.get_mut().flush()?; | ||
|
||
if options.noreply { | ||
|
@@ -424,7 +419,7 @@ impl AsciiProtocol<Stream> { | |
if !buf.starts_with("VALUE") { | ||
return Err(ServerError::BadResponse(Cow::Owned(buf.into())))?; | ||
} | ||
let mut header = buf.trim_end_matches("\r\n").split(" "); | ||
let mut header = buf.trim_end_matches("\r\n").split(' '); | ||
let mut next_or_err = || { | ||
header | ||
.next() | ||
|
@@ -481,7 +476,7 @@ mod tests { | |
match self.reads.pop_front() { | ||
Some(range) => { | ||
let range = &self.data[range]; | ||
(&mut buf[0..range.len()]).copy_from_slice(&range); | ||
buf[0..range.len()].copy_from_slice(range); | ||
Ok(range.len()) | ||
} | ||
None => Err(std::io::ErrorKind::WouldBlock.into()), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip
Codebase Verification
The extracted content from the test
test_capped_line_reader
in thesrc/protocol/ascii.rs
file shows that the test primarily focuses on the functionality of reading lines correctly using theCappedLineReader
. The test uses a mock reader to simulate input and checks if theCappedLineReader
can correctly parse lines from the input. However, there is no indication that this test assesses memory usage or performance implications of using aVec<u8>
instead of a fixed-size array.Given the lack of specific tests or benchmarks addressing the memory usage or performance after changing to
Vec<u8>
, we cannot conclusively verify that there are no unintended side effects such as increased memory usage or performance degradation due to this change. It would be advisable to implement or look for additional benchmarks or profiling tests that specifically measure these aspects.Analysis chain
Replacing fixed-size array with
Vec<u8>
inCappedLineReader
struct allows for more flexible buffer management. Ensure that there are no unintended side effects such as increased memory usage or performance degradation.The search results from the
rg
command indicate that theCappedLineReader
struct is indeed present in thesrc/protocol/ascii.rs
file and is used in various contexts, including instantiation and method implementations. However, the output does not provide direct evidence regarding the impact of replacing a fixed-size array withVec<u8>
on memory usage or performance.To further verify the impact of this change, we need to examine the usage patterns of
CappedLineReader
and possibly look for any benchmarks or tests that measure memory usage or performance. This will help us determine if there are any reported or observable issues related to the change.Scripts Executed
The following scripts were executed for the analysis:
Script:
Length of output: 365
Script:
Length of output: 223
Script:
Length of output: 786