Copilot commented on code in PR #3638:
URL: https://github.com/apache/thrift/pull/3638#discussion_r3594158585


##########
lib/rs/src/transport/shared.rs:
##########
@@ -0,0 +1,85 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::io::{self, Read, Write};
+use std::sync::{Arc, Mutex, MutexGuard};
+
+use super::{ReadHalf, TIoChannel, WriteHalf};
+
+/// A clonable channel that serializes access to an underlying I/O stream.
+///
+/// This adapter allows a bidirectional stream that cannot be cloned, such as a
+/// TLS session, to implement [`TIoChannel`]. Every read, write, and flush 
holds
+/// the same lock for the duration of that operation. It is intended for
+/// synchronous request-response traffic, where a caller writes and flushes a
+/// complete request before reading its response.
+///
+/// The shared lock makes access memory-safe across threads, but it does not
+/// provide full-duplex progress: a blocking read holds the lock and prevents a
+/// concurrent write until that read finishes.
+#[derive(Debug)]
+pub struct TSharedChannel<C> {
+    inner: Arc<Mutex<C>>,
+}
+
+impl<C> TSharedChannel<C> {
+    /// Wrap `inner` in a shared channel.
+    pub fn new(inner: C) -> Self {
+        Self {
+            inner: Arc::new(Mutex::new(inner)),
+        }
+    }
+
+    pub(crate) fn lock(&self) -> io::Result<MutexGuard<'_, C>> {
+        self.inner
+            .lock()
+            .map_err(|_| io::Error::from(io::ErrorKind::Other))
+    }
+}

Review Comment:
   When the mutex is poisoned, `lock()` currently returns 
`io::ErrorKind::Other` with the default message (typically just "other error"), 
which is hard to diagnose. Returning a specific message makes downstream 
`TransportError.message` much more actionable without changing control flow.



##########
lib/rs/src/transport/shared.rs:
##########
@@ -0,0 +1,85 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::io::{self, Read, Write};
+use std::sync::{Arc, Mutex, MutexGuard};
+
+use super::{ReadHalf, TIoChannel, WriteHalf};
+
+/// A clonable channel that serializes access to an underlying I/O stream.

Review Comment:
   Doc comment uses nonstandard spelling "clonable"; use "cloneable" for 
clarity/consistency with Rust terminology.



##########
lib/rs/tests/shared_channel.rs:
##########
@@ -0,0 +1,97 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::VecDeque;
+use std::io::{self, Read, Write};
+use std::panic::{catch_unwind, AssertUnwindSafe};
+use std::sync::{Arc, Mutex};
+
+use thrift::transport::{TIoChannel, TSharedChannel};
+
+struct TestIo {
+    readable: VecDeque<u8>,
+    written: Arc<Mutex<Vec<u8>>>,
+}
+
+impl Read for TestIo {
+    fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
+        let count = buffer.len().min(self.readable.len());
+        for target in &mut buffer[..count] {
+            *target = self.readable.pop_front().unwrap();
+        }
+        Ok(count)
+    }
+}
+
+impl Write for TestIo {
+    fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
+        self.written.lock().unwrap().extend_from_slice(buffer);
+        Ok(buffer.len())
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        Ok(())
+    }
+}
+
+#[test]
+fn split_supports_an_inner_channel_that_is_not_clone() {

Review Comment:
   Test name reads awkwardly ("is_not_clone") and is easy to misread; using 
"cloneable" matches the trait name and makes the intent clearer.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to