From b673542fa8f3c5b56be0c51d1077b75b208fe10f Mon Sep 17 00:00:00 2001 From: James Sturtevant Date: Thu, 21 Mar 2024 17:51:55 +0000 Subject: [PATCH 1/4] Use rust-toolchain.toml and newer rust version Use rust-toolchain.toml file over makefile installation. Signed-off-by: James Sturtevant --- .github/workflows/bvt.yml | 2 -- Makefile | 8 -------- rust-toolchain.toml | 4 ++++ 3 files changed, 4 insertions(+), 10 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/.github/workflows/bvt.yml b/.github/workflows/bvt.yml index e9917550..af542e33 100644 --- a/.github/workflows/bvt.yml +++ b/.github/workflows/bvt.yml @@ -12,7 +12,6 @@ jobs: uses: actions/checkout@v3 - name: Check run: | - make deps make check make -C compiler check make -C ttrpc-codegen check @@ -28,7 +27,6 @@ jobs: uses: actions/checkout@v3 - name: Build run: | - make deps make make -C compiler make -C ttrpc-codegen diff --git a/Makefile b/Makefile index b0b3082e..97e058b3 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,3 @@ -RUST_VERSION = 1.66 - all: debug test # @@ -34,9 +32,3 @@ endif check: cargo fmt --all -- --check cargo clippy --all-targets --all-features -- -D warnings - -.PHONY: deps -deps: - rustup install $(RUST_VERSION) - rustup default $(RUST_VERSION) - rustup component add rustfmt clippy diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 00000000..c058c59e --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel="1.77.0" +profile="default" +components=["rustfmt", "clippy"] From e1ba6f81c533d8112e29f224eeae3fbd15b5be4c Mon Sep 17 00:00:00 2001 From: James Sturtevant Date: Thu, 21 Mar 2024 19:09:26 +0000 Subject: [PATCH 2/4] Add check-all command Add convenient helper make function and use it in CI Signed-off-by: James Sturtevant --- .github/workflows/bvt.yml | 4 +--- Makefile | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/bvt.yml b/.github/workflows/bvt.yml index af542e33..ba6f519d 100644 --- a/.github/workflows/bvt.yml +++ b/.github/workflows/bvt.yml @@ -12,9 +12,7 @@ jobs: uses: actions/checkout@v3 - name: Check run: | - make check - make -C compiler check - make -C ttrpc-codegen check + make check-all make: name: Build diff --git a/Makefile b/Makefile index 97e058b3..c4956538 100644 --- a/Makefile +++ b/Makefile @@ -32,3 +32,9 @@ endif check: cargo fmt --all -- --check cargo clippy --all-targets --all-features -- -D warnings + +.PHONY: check-all +check-all: + $(MAKE) check + $(MAKE) -C compiler check + $(MAKE) -C ttrpc-codegen check From 8d8ac56661212471ff2764f29f0ee6a5a85e44d4 Mon Sep 17 00:00:00 2001 From: James Sturtevant Date: Thu, 21 Mar 2024 17:52:40 +0000 Subject: [PATCH 3/4] Fix clippy suggestion Fix clippy suggestion Signed-off-by: James Sturtevant Signed-off-by: James Sturtevant --- compiler/src/util.rs | 2 +- src/asynchronous/shutdown.rs | 2 +- src/sync/sys/windows/net.rs | 40 ++++++++++++++++++------------------ ttrpc-codegen/src/lib.rs | 1 + ttrpc-codegen/src/model.rs | 9 ++------ ttrpc-codegen/src/parser.rs | 16 +++++++-------- 6 files changed, 33 insertions(+), 37 deletions(-) diff --git a/compiler/src/util.rs b/compiler/src/util.rs index 4cb194d5..42bc30e0 100644 --- a/compiler/src/util.rs +++ b/compiler/src/util.rs @@ -48,7 +48,7 @@ impl<'a> Iterator for NameSpliter<'a> { let mut meet_lower = false; for i in self.pos..self.name.len() { let c = self.name[i]; - if (b'A'..=b'Z').contains(&c) { + if c.is_ascii_uppercase() { if meet_lower { // So it should be AaA or aaA pos = i; diff --git a/src/asynchronous/shutdown.rs b/src/asynchronous/shutdown.rs index 0bcf85c7..6ea6a92c 100644 --- a/src/asynchronous/shutdown.rs +++ b/src/asynchronous/shutdown.rs @@ -305,7 +305,7 @@ mod test { }); notifier.shutdown(); // Elapsed - assert!(matches!(notifier.wait_all_exit().await, Err(_))); + assert!(notifier.wait_all_exit().await.is_err()); task.await.unwrap(); } } diff --git a/src/sync/sys/windows/net.rs b/src/sync/sys/windows/net.rs index e416c189..87fcd002 100644 --- a/src/sync/sys/windows/net.rs +++ b/src/sync/sys/windows/net.rs @@ -112,7 +112,7 @@ impl PipeListener { let res = unsafe {GetOverlappedResult(np.named_pipe, ol.as_mut_ptr(), &mut bytes_transfered, WAIT_FOR_EVENT) }; match res { 0 => { - return Err(io::Error::last_os_error()); + Err(io::Error::last_os_error()) } _ => { if let Some(shutdown_signal) = self.handle_shutdown(&np) { @@ -129,10 +129,10 @@ impl PipeListener { Ok(Some(np)) } e => { - return Err(io::Error::new( + Err(io::Error::new( io::ErrorKind::Other, format!("failed to connect pipe: {:?}", e), - )); + )) } } } @@ -165,12 +165,12 @@ impl PipeListener { // https://learn.microsoft.com/en-us/windows/win32/ipc/named-pipe-security-and-access-rights match unsafe { CreateNamedPipeW(name.as_ptr(), open_mode, PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 0, std::ptr::null_mut())} { INVALID_HANDLE_VALUE => { - return Err(io::Error::last_os_error()) + Err(io::Error::last_os_error()) } h => { - return Ok(h) + Ok(h) }, - }; + } } pub fn close(&self) -> Result<()> { @@ -208,8 +208,8 @@ impl PipeConnection { let write_event = create_event()?; Ok(PipeConnection { named_pipe: h, - read_event: read_event, - write_event: write_event, + read_event, + write_event, }) } @@ -236,15 +236,15 @@ impl PipeConnection { let res = unsafe {GetOverlappedResult(self.named_pipe, ol.as_mut_ptr(), &mut bytes_transfered, WAIT_FOR_EVENT) }; match res { 0 => { - return Err(handle_windows_error(io::Error::last_os_error())) + Err(handle_windows_error(io::Error::last_os_error())) } _ => { - return Ok(bytes_transfered as usize) + Ok(bytes_transfered as usize) } } } ref e => { - return Err(Error::Others(format!("failed to read from pipe: {:?}", e))) + Err(Error::Others(format!("failed to read from pipe: {:?}", e))) } } } @@ -267,15 +267,15 @@ impl PipeConnection { let res = unsafe {GetOverlappedResult(self.named_pipe, ol.as_mut_ptr(), &mut bytes_transfered, WAIT_FOR_EVENT) }; match res { 0 => { - return Err(handle_windows_error(io::Error::last_os_error())) + Err(handle_windows_error(io::Error::last_os_error())) } _ => { - return Ok(bytes_transfered as usize) + Ok(bytes_transfered as usize) } } } ref e => { - return Err(Error::Others(format!("failed to write to pipe: {:?}", e))) + Err(Error::Others(format!("failed to write to pipe: {:?}", e))) } } } @@ -346,10 +346,10 @@ impl ClientConnection { .custom_flags(FILE_FLAG_OVERLAPPED); match opts.open(self.address.as_str()) { Ok(file) => { - return PipeConnection::new(file.into_raw_handle() as isize) + PipeConnection::new(file.into_raw_handle() as isize) } Err(e) => { - return Err(handle_windows_error(e)) + Err(handle_windows_error(e)) } } } @@ -383,7 +383,7 @@ mod test { let client = ClientConnection::new("non_existent_pipe"); match client.get_pipe_connection() { Ok(_) => { - assert!(false, "should not be able to get a connection to a non existent pipe"); + panic!("should not be able to get a connection to a non existent pipe"); } Err(e) => { assert_eq!(e, Error::Windows(ERROR_FILE_NOT_FOUND as i32)); @@ -404,10 +404,10 @@ mod test { // pipe is working } Ok(None) => { - assert!(false, "should get a working pipe") + panic!("should get a working pipe") } Err(e) => { - assert!(false, "should not get error {}", e.to_string()) + panic!("should not get error {}", e) } } }); @@ -425,7 +425,7 @@ mod test { let quit_flag = Arc::new(AtomicBool::new(false)); match listener_server.accept(&quit_flag) { Ok(_) => { - assert!(false, "should not get pipe on close") + panic!("should not get pipe on close") } Err(e) => { assert_eq!(e.to_string(), "closing pipe") diff --git a/ttrpc-codegen/src/lib.rs b/ttrpc-codegen/src/lib.rs index 31b38d19..559e48b8 100644 --- a/ttrpc-codegen/src/lib.rs +++ b/ttrpc-codegen/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] //! API to generate .rs files for ttrpc from protobuf //! //! diff --git a/ttrpc-codegen/src/model.rs b/ttrpc-codegen/src/model.rs index 5156e864..b60bf72e 100644 --- a/ttrpc-codegen/src/model.rs +++ b/ttrpc-codegen/src/model.rs @@ -8,20 +8,15 @@ use crate::str_lit::StrLit; use protobuf_support::lexer::float; /// Protobox syntax -#[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] pub enum Syntax { /// Protobuf syntax [2](https://developers.google.com/protocol-buffers/docs/proto) (default) + #[default] Proto2, /// Protobuf syntax [3](https://developers.google.com/protocol-buffers/docs/proto3) Proto3, } -impl Default for Syntax { - fn default() -> Syntax { - Syntax::Proto2 - } -} - /// A field rule #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum Rule { diff --git a/ttrpc-codegen/src/parser.rs b/ttrpc-codegen/src/parser.rs index e3736d23..16fba7a1 100644 --- a/ttrpc-codegen/src/parser.rs +++ b/ttrpc-codegen/src/parser.rs @@ -387,11 +387,11 @@ impl<'a> Lexer<'a> { // capitalLetter = "A" … "Z" fn _next_capital_letter_opt(&mut self) -> Option { - self.next_char_if(|c| ('A'..='Z').contains(&c)) + self.next_char_if(|c| c.is_ascii_uppercase()) } fn is_ascii_alphanumeric(c: char) -> bool { - ('a'..='z').contains(&c) || ('A'..='Z').contains(&c) || ('0'..='9').contains(&c) + c.is_ascii_lowercase() || c.is_ascii_uppercase() || c.is_ascii_digit() } fn next_ident_part(&mut self) -> Option { @@ -417,7 +417,7 @@ impl<'a> Lexer<'a> { // Integer literals fn is_ascii_hexdigit(c: char) -> bool { - ('0'..='9').contains(&c) || ('a'..='f').contains(&c) || ('A'..='F').contains(&c) + c.is_ascii_digit() || ('a'..='f').contains(&c) || ('A'..='F').contains(&c) } // hexLit = "0" ( "x" | "X" ) hexDigit { hexDigit } @@ -433,7 +433,7 @@ impl<'a> Lexer<'a> { } fn is_ascii_digit(c: char) -> bool { - ('0'..='9').contains(&c) + c.is_ascii_digit() } // decimalLit = ( "1" … "9" ) { decimalDigit } @@ -458,7 +458,7 @@ impl<'a> Lexer<'a> { fn next_hex_digit(&mut self) -> ParserResult { let mut clone = *self; let r = match clone.next_char()? { - c if ('0'..='9').contains(&c) => c as u32 - b'0' as u32, + c if c.is_ascii_digit() => c as u32 - b'0' as u32, c if ('A'..='F').contains(&c) => c as u32 - b'A' as u32 + 10, c if ('a'..='f').contains(&c) => c as u32 - b'a' as u32 + 10, _ => return Err(ParserError::ExpectHexDigit), @@ -482,7 +482,7 @@ impl<'a> Lexer<'a> { fn next_decimal_digit(&mut self) -> ParserResult { let mut clone = *self; let r = match clone.next_char()? { - c if ('0'..='9').contains(&c) => c as u32 - '0' as u32, + c if c.is_ascii_digit() => c as u32 - '0' as u32, _ => return Err(ParserError::ExpectDecDigit), }; *self = clone; @@ -492,7 +492,7 @@ impl<'a> Lexer<'a> { // decimals = decimalDigit { decimalDigit } fn next_decimal_digits(&mut self) -> ParserResult<()> { self.next_decimal_digit()?; - self.take_while(|c| ('0'..='9').contains(&c)); + self.take_while(|c| c.is_ascii_digit()); Ok(()) } @@ -1029,7 +1029,7 @@ impl<'a> Parser<'a> { } fn is_ascii_uppercase(c: char) -> bool { - ('A'..='Z').contains(&c) + c.is_ascii_uppercase() } // groupName = capitalLetter { letter | decimalDigit | "_" } From 486b4e826dc0cdda9cea6b1c647542f3a57f5e56 Mon Sep 17 00:00:00 2001 From: James Sturtevant Date: Fri, 29 Mar 2024 23:46:37 +0000 Subject: [PATCH 4/4] Add rust min version Add a rust minimal version Signed-off-by: James Sturtevant --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index e4fcb0e4..78ba208a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ readme = "README.md" repository = "https://github.com/containerd/ttrpc-rust" homepage = "https://github.com/containerd/ttrpc-rust" description = "A Rust version of ttrpc." +rust-version = "1.70" [dependencies] protobuf = { version = "3.1.0" }