From 85f725a82f8d3f9b0d7442d698bbca40f124e978 Mon Sep 17 00:00:00 2001 From: Fantix King Date: Sat, 11 Apr 2026 10:37:10 -0400 Subject: [PATCH 1/4] chore: rustfmt like Compio --- compio-py-dynamic-openssl/src/ssl.rs | 8 +- compio-py/src/event_loop.rs | 8 +- compio-py/src/import.rs | 37 ++++--- compio-py/src/owned.rs | 142 +++++++++++++++------------ compio-py/src/runtime.rs | 9 +- compio-py/src/send_wrapper.rs | 3 +- compio-py/src/thread.rs | 26 +++-- rustfmt.toml | 19 ++++ 8 files changed, 152 insertions(+), 100 deletions(-) create mode 100644 rustfmt.toml diff --git a/compio-py-dynamic-openssl/src/ssl.rs b/compio-py-dynamic-openssl/src/ssl.rs index 14539b1..be0d7ff 100644 --- a/compio-py-dynamic-openssl/src/ssl.rs +++ b/compio-py-dynamic-openssl/src/ssl.rs @@ -398,12 +398,12 @@ mod error { pub struct ErrorCode(c_int); impl ErrorCode { - pub const ZERO_RETURN: ErrorCode = ErrorCode(ffi::SSL_ERROR_ZERO_RETURN); - pub const WANT_READ: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_READ); - pub const WANT_WRITE: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_WRITE); - pub const SYSCALL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SYSCALL); pub const SSL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SSL); + pub const SYSCALL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SYSCALL); pub const WANT_CLIENT_HELLO_CB: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_CLIENT_HELLO_CB); + pub const WANT_READ: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_READ); + pub const WANT_WRITE: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_WRITE); + pub const ZERO_RETURN: ErrorCode = ErrorCode(ffi::SSL_ERROR_ZERO_RETURN); pub fn from_raw(raw: c_int) -> ErrorCode { ErrorCode(raw) diff --git a/compio-py/src/event_loop.rs b/compio-py/src/event_loop.rs index aba5438..eb03a98 100644 --- a/compio-py/src/event_loop.rs +++ b/compio-py/src/event_loop.rs @@ -13,8 +13,7 @@ use once_cell::sync::OnceCell; use pyo3::{ IntoPyObjectExt, buffer::PyBuffer, - exceptions::PyRuntimeError, - exceptions::PyValueError, + exceptions::{PyRuntimeError, PyValueError}, ffi::c_str, prelude::*, types::{PyDict, PyMapping, PyTuple, PyWeakrefReference}, @@ -284,8 +283,9 @@ impl CompioLoop { } } - /// Spawn a Rust Future onto the event loop, returning a connected Python Future. - /// When the Python Future is cancelled, the Rust Future is also cancelled. + /// Spawn a Rust Future onto the event loop, returning a connected Python + /// Future. When the Python Future is cancelled, the Rust Future is also + /// cancelled. pub fn spawn_py<'py, F>(&self, py: Python<'py>, fut: F) -> PyResult> where F: Future>> + 'static, diff --git a/compio-py/src/import.rs b/compio-py/src/import.rs index d26ee26..771aa0f 100644 --- a/compio-py/src/import.rs +++ b/compio-py/src/import.rs @@ -3,20 +3,21 @@ //! Python module import utilities with caching. //! -//! This module provides macros and functions for efficiently importing Python modules -//! and accessing their attributes with automatic caching using `OnceCell`. +//! This module provides macros and functions for efficiently importing Python +//! modules and accessing their attributes with automatic caching using +//! `OnceCell`. //! //! # Thread Safety //! -//! All cached imports and attributes use `OnceCell::sync`, making them safe to access -//! from multiple threads. The first thread to access will perform the import/getattr, -//! and subsequent accesses will use the cached value. +//! All cached imports and attributes use `OnceCell::sync`, making them safe to +//! access from multiple threads. The first thread to access will perform the +//! import/getattr, and subsequent accesses will use the cached value. //! //! # Performance //! -//! By caching module imports and attribute lookups, this module avoids the overhead -//! of repeated Python import statements and attribute resolution, which can be -//! significant in performance-critical code paths. +//! By caching module imports and attribute lookups, this module avoids the +//! overhead of repeated Python import statements and attribute resolution, +//! which can be significant in performance-critical code paths. //! //! # Example //! @@ -34,8 +35,9 @@ use pyo3::prelude::*; /// Generates a function that imports a Python module with caching. /// -/// This macro creates a public function that imports the specified Python module -/// on first call and caches it for subsequent calls using a static `OnceCell`. +/// This macro creates a public function that imports the specified Python +/// module on first call and caches it for subsequent calls using a static +/// `OnceCell`. /// /// # Generated Function Signature /// @@ -45,7 +47,8 @@ use pyo3::prelude::*; /// /// # Arguments /// -/// * `$module_name` - The name of the Python module to import (as an identifier) +/// * `$module_name` - The name of the Python module to import (as an +/// identifier) /// /// # Example /// @@ -60,8 +63,8 @@ use pyo3::prelude::*; /// /// # Thread Safety /// -/// The generated function is thread-safe. Multiple threads can call it concurrently, -/// and only the first call will perform the actual import. +/// The generated function is thread-safe. Multiple threads can call it +/// concurrently, and only the first call will perform the actual import. macro_rules! import { ($module_name:ident) => { pub fn $module_name<'py>(py: Python<'py>) -> PyResult<&'py Bound<'py, PyModule>> { @@ -105,7 +108,8 @@ macro_rules! import { /// /// # Thread Safety /// -/// Like `import!`, this macro generates thread-safe code using static `OnceCell`. +/// Like `import!`, this macro generates thread-safe code using static +/// `OnceCell`. /// /// # Note /// @@ -121,8 +125,9 @@ macro_rules! getattr { /// Creates a module wrapper with standard imports and boilerplate. /// -/// This macro combines the `import!` macro with a module definition that includes -/// the standard `use` statements needed for working with Python objects. +/// This macro combines the `import!` macro with a module definition that +/// includes the standard `use` statements needed for working with Python +/// objects. /// /// # Arguments /// diff --git a/compio-py/src/owned.rs b/compio-py/src/owned.rs index c55f23a..655f81f 100644 --- a/compio-py/src/owned.rs +++ b/compio-py/src/owned.rs @@ -3,10 +3,10 @@ //! Thread-safe cell with single-owner access control and reference counting. //! -//! This module provides `OwnedRefCell`, which combines lazy initialization with -//! exclusive ownership semantics. At any given time, at most one thread "owns" the -//! cell and can access its contents. The owning thread can hold multiple references -//! simultaneously through reference counting. +//! This module provides `OwnedRefCell`, which combines lazy initialization +//! with exclusive ownership semantics. At any given time, at most one thread +//! "owns" the cell and can access its contents. The owning thread can hold +//! multiple references simultaneously through reference counting. //! //! This module is inspired by: //! * `std::cell::RefCell` for interior mutability and reference counting @@ -14,8 +14,6 @@ //! * https://github.com/compio-rs/compio/blob/master/compio-runtime/src/runtime/send_wrapper.rs //! * https://github.com/thk1/send_wrapper -use pyo3::PyErr; -use pyo3::exceptions::PyRuntimeError; use std::{ cell::{Cell, UnsafeCell}, marker, mem, @@ -23,6 +21,8 @@ use std::{ sync::atomic::{AtomicU32, Ordering}, }; +use pyo3::{PyErr, exceptions::PyRuntimeError}; + /// Sentinel value indicating that no thread currently owns the cell. const NO_OWNER: u32 = 0; @@ -55,20 +55,24 @@ type OwnedResult = Result; /// A thread-safe cell with single-owner access control. /// -/// `OwnedRefCell` provides synchronized access to an optional value with the following -/// guarantees: +/// `OwnedRefCell` provides synchronized access to an optional value with the +/// following guarantees: /// /// - **Single ownership**: At most one thread can own the cell at any time -/// - **Reference counting**: The owning thread can create multiple `Ref` instances -/// - **Reentrant control**: Callers can specify whether reentrant access is allowed -/// - **Lazy initialization**: The value can be initialized exactly once after creation +/// - **Reference counting**: The owning thread can create multiple `Ref` +/// instances +/// - **Reentrant control**: Callers can specify whether reentrant access is +/// allowed +/// - **Lazy initialization**: The value can be initialized exactly once after +/// creation /// /// # Thread Safety /// -/// The cell is `Sync` when `T: Send`. The `owner` atomic ensures exclusive ownership, -/// while the `borrow` counter tracks active references from the owning thread. Since -/// only the owning thread can access `borrow` (protected by the `owner` lock), using -/// a non-atomic `Cell` is sound and more efficient than `AtomicUsize`. +/// The cell is `Sync` when `T: Send`. The `owner` atomic ensures exclusive +/// ownership, while the `borrow` counter tracks active references from the +/// owning thread. Since only the owning thread can access `borrow` (protected +/// by the `owner` lock), using a non-atomic `Cell` is sound and more efficient +/// than `AtomicUsize`. /// /// # Examples /// @@ -87,8 +91,8 @@ type OwnedResult = Result; /// ``` pub struct OwnedRefCell { /// The inner storage for the optional value. - /// Uses `UnsafeCell` to allow interior mutability while maintaining `Sync` safety - /// through the owner lock. + /// Uses `UnsafeCell` to allow interior mutability while maintaining `Sync` + /// safety through the owner lock. inner: UnsafeCell>, /// Atomic tracking of which thread (by thread ID) currently owns this cell. @@ -96,8 +100,8 @@ pub struct OwnedRefCell { owner: AtomicU32, /// Reference counter tracking the number of active `Ref` instances. - /// This is a `Cell` (not `AtomicUsize`) because only the owning thread can access it, - /// with access protected by the `owner` atomic lock. + /// This is a `Cell` (not `AtomicUsize`) because only the owning thread can + /// access it, with access protected by the `owner` atomic lock. borrow: Cell, } @@ -108,8 +112,10 @@ impl OwnedRefCell { /// /// This is safe because: /// - Reads are protected by the atomic `owner` field - /// - Modifications only occur when a thread holds ownership via `OwnershipGuard` - /// - The borrow counter ensures the value isn't dropped while references exist + /// - Modifications only occur when a thread holds ownership via + /// `OwnershipGuard` + /// - The borrow counter ensures the value isn't dropped while references + /// exist #[inline] fn get_unchecked(&self) -> Option<&T> { unsafe { &*self.inner.get() }.as_ref() @@ -117,19 +123,21 @@ impl OwnedRefCell { /// Attempts to acquire ownership of this cell for the current thread. /// - /// This method acquires an `OwnershipGuard` which increments the reference count. - /// When all `OwnershipGuard` instances are dropped, ownership is released. + /// This method acquires an `OwnershipGuard` which increments the reference + /// count. When all `OwnershipGuard` instances are dropped, ownership is + /// released. /// /// # Parameters /// - /// - `reentrant`: If `true`, allows the current owner to acquire again (increments ref count). - /// If `false`, returns `Err` if the cell is already owned. + /// - `reentrant`: If `true`, allows the current owner to acquire again + /// (increments ref count). If `false`, returns `Err` if the cell is + /// already owned. /// /// # Returns /// /// - `Ok(OwnershipGuard)` if ownership was acquired or reacquired - /// - `Err(thread_id)` if another thread owns the cell, or if `reentrant=false` and - /// this thread already owns it + /// - `Err(thread_id)` if another thread owns the cell, or if + /// `reentrant=false` and this thread already owns it /// /// # Examples /// @@ -150,8 +158,9 @@ impl OwnedRefCell { /// Gets a reference to the value in the cell, if it exists. /// - /// This is a convenience method that acquires ownership (allowing reentrant access) - /// and returns a reference to the value if it has been initialized. + /// This is a convenience method that acquires ownership (allowing reentrant + /// access) and returns a reference to the value if it has been + /// initialized. /// /// # Returns /// @@ -166,8 +175,8 @@ impl OwnedRefCell { /// Initializes the cell with a value. /// - /// The calling thread must acquire ownership (or already own it) before initializing. - /// This method allows reentrant access. + /// The calling thread must acquire ownership (or already own it) before + /// initializing. This method allows reentrant access. /// /// # Panics /// @@ -226,23 +235,24 @@ impl Default for OwnedRefCell { /// - The `owner` atomic ensures exclusive ownership (only one thread at a time) /// - The `borrow` counter is only accessed by the owning thread /// - The value can be sent between threads when ownership is transferred -/// - After initialization, the value is immutable (only read through shared references) -// FIXME: since compio-rs/compio#660, `Proactor` is no longer `Send`, so we tentatively -// allow any `T` to be accessible from multiple threads, leaving undefined behavior -// if `T` is not `Send`. A proper fix would be to explicitly pin the thread ID by the -// runtime when it is known to be safe. See also #6. +/// - After initialization, the value is immutable (only read through shared +/// references) +// FIXME: since compio-rs/compio#660, `Proactor` is no longer `Send`, so we +// tentatively allow any `T` to be accessible from multiple threads, leaving +// undefined behavior if `T` is not `Send`. A proper fix would be to explicitly +// pin the thread ID by the runtime when it is known to be safe. See also #6. // unsafe impl Sync for OwnedRefCell where T: Send {} unsafe impl Sync for OwnedRefCell {} unsafe impl Send for OwnedRefCell {} /// A reference to a value inside an `OwnedRefCell`. /// -/// This type derefs to `T` and holds an `OwnershipGuard` to maintain the reference count. -/// When dropped, the reference count is decremented, and ownership may be released -/// if this was the last reference. +/// This type derefs to `T` and holds an `OwnershipGuard` to maintain the +/// reference count. When dropped, the reference count is decremented, and +/// ownership may be released if this was the last reference. /// -/// `Ref` is neither `Send` nor `Sync` because it must be dropped on the same thread -/// that created it to maintain correct ownership tracking. +/// `Ref` is neither `Send` nor `Sync` because it must be dropped on the same +/// thread that created it to maintain correct ownership tracking. pub struct Ref<'a, T> { /// Reference to the value stored in the cell. value: &'a T, @@ -260,19 +270,21 @@ impl Deref for Ref<'_, T> { } } -/// RAII guard that manages ownership and reference counting for an `OwnedRefCell`. +/// RAII guard that manages ownership and reference counting for an +/// `OwnedRefCell`. /// -/// Each `OwnershipGuard` increments the borrow counter when created and decrements it when -/// dropped. Ownership is released (reset to `NO_OWNER`) only when the reference count -/// reaches zero. +/// Each `OwnershipGuard` increments the borrow counter when created and +/// decrements it when dropped. Ownership is released (reset to `NO_OWNER`) only +/// when the reference count reaches zero. /// -/// `OwnershipGuard` is neither `Send` nor `Sync` (via `PhantomData<*const ()>`) because -/// it must be dropped on the same thread that created it to maintain correct ownership -/// tracking and borrow counting. +/// `OwnershipGuard` is neither `Send` nor `Sync` (via `PhantomData<*const ()>`) +/// because it must be dropped on the same thread that created it to maintain +/// correct ownership tracking and borrow counting. pub struct OwnershipGuard<'a> { /// Reference to the atomic owner field tracking which thread owns the cell. owner: &'a AtomicU32, - /// Reference to the non-atomic borrow counter (protected by the owner lock). + /// Reference to the non-atomic borrow counter (protected by the owner + /// lock). borrow: &'a Cell, /// Marker type to make this `!Send` and `!Sync`. /// Using `*const ()` because raw pointers are neither `Send` nor `Sync`. @@ -280,9 +292,11 @@ pub struct OwnershipGuard<'a> { } impl<'a> OwnershipGuard<'a> { - /// Attempts to create a new `OwnershipGuard` by acquiring or reacquiring ownership. + /// Attempts to create a new `OwnershipGuard` by acquiring or reacquiring + /// ownership. /// - /// This method uses a compare-and-swap loop to atomically manage the owner field. + /// This method uses a compare-and-swap loop to atomically manage the owner + /// field. /// /// # Parameters /// @@ -293,8 +307,8 @@ impl<'a> OwnershipGuard<'a> { /// # Returns /// /// - `Ok(OwnershipGuard)` if ownership was acquired or reacquired - /// - `Err(thread_id)` if another thread owns the cell, or if `reentrant=false` - /// and this thread already owns it + /// - `Err(thread_id)` if another thread owns the cell, or if + /// `reentrant=false` and this thread already owns it #[inline] fn new( owner: &'a AtomicU32, @@ -312,7 +326,8 @@ impl<'a> OwnershipGuard<'a> { while old == NO_OWNER { // Attempt to atomically swap NO_OWNER with our thread ID // Success ordering: AcqRel - // - Acquire: synchronize with previous Release store (though NO_OWNER means none) + // - Acquire: synchronize with previous Release store (though NO_OWNER means + // none) // - Release: publish our ownership to other threads // Failure ordering: Acquire // - Synchronize with concurrent modifications @@ -342,14 +357,16 @@ impl<'a> OwnershipGuard<'a> { /// /// # Safety /// - /// This must only be called after successfully acquiring ownership (either via CAS - /// or reentrant check). The caller must ensure that only the owning thread calls this. + /// This must only be called after successfully acquiring ownership (either + /// via CAS or reentrant check). The caller must ensure that only the + /// owning thread calls this. /// /// # Note on `Cell` safety /// /// Using `Cell::get()` and `Cell::set()` is safe here because: /// - Only the owning thread can call this (protected by the `owner` lock) - /// - The `OwnershipGuard` is `!Send`, so it can't be transferred to another thread + /// - The `OwnershipGuard` is `!Send`, so it can't be transferred to another + /// thread /// - This avoids the overhead of atomic operations while maintaining safety #[inline] fn new_unchecked(owner: &'a AtomicU32, borrow: &'a Cell) -> OwnershipGuard<'a> { @@ -367,9 +384,10 @@ impl<'a> OwnershipGuard<'a> { impl Drop for OwnershipGuard<'_> { /// Decrements the reference count when the guard is dropped. /// - /// If the count reaches zero, releases ownership by resetting the owner field - /// to `NO_OWNER`. Uses Release ordering to ensure all modifications made while - /// holding ownership are visible to the next thread that acquires it. + /// If the count reaches zero, releases ownership by resetting the owner + /// field to `NO_OWNER`. Uses Release ordering to ensure all + /// modifications made while holding ownership are visible to the next + /// thread that acquires it. #[inline] fn drop(&mut self) { // Decrement the borrow count @@ -388,9 +406,9 @@ impl Drop for OwnershipGuard<'_> { #[cfg(test)] mod tests { + use std::{sync::Arc, thread}; + use super::*; - use std::sync::Arc; - use std::thread; #[test] fn test_init_and_get() { diff --git a/compio-py/src/runtime.rs b/compio-py/src/runtime.rs index 15a8116..5690392 100644 --- a/compio-py/src/runtime.rs +++ b/compio-py/src/runtime.rs @@ -13,8 +13,7 @@ use std::{ Arc, atomic::{self, AtomicBool, AtomicUsize}, }, - task::Waker, - task::{Context, Poll}, + task::{Context, Poll, Waker}, time::{Duration, Instant}, }; @@ -28,7 +27,11 @@ use compio::{ }; use compio_executor::{Executor, JoinHandle}; use compio_log::*; -use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyDict, types::PyWeakrefReference}; +use pyo3::{ + exceptions::PyRuntimeError, + prelude::*, + types::{PyDict, PyWeakrefReference}, +}; /// Minimum number of _scheduled timer handles before cleanup of /// cancelled handles is performed. diff --git a/compio-py/src/send_wrapper.rs b/compio-py/src/send_wrapper.rs index 7eb2a41..9608895 100644 --- a/compio-py/src/send_wrapper.rs +++ b/compio-py/src/send_wrapper.rs @@ -96,7 +96,8 @@ impl Drop for SendWrapper { #[inline(never)] #[track_caller] fn invalid_drop() { - const DROP_ERROR: &'static str = "Dropped SendWrapper variable from a thread different to the one it has been created with."; + const DROP_ERROR: &'static str = "Dropped SendWrapper variable from a thread different to \ + the one it has been created with."; if !thread::panicking() { // panic because of dropping from wrong thread diff --git a/compio-py/src/thread.rs b/compio-py/src/thread.rs index 80e7475..e50e4ee 100644 --- a/compio-py/src/thread.rs +++ b/compio-py/src/thread.rs @@ -3,13 +3,14 @@ //! Thread ID utilities for atomic storage. //! -//! Provides a numeric representation of thread IDs that can be stored in atomic integers -//! for ownership tracking and cross-thread validation. +//! Provides a numeric representation of thread IDs that can be stored in atomic +//! integers for ownership tracking and cross-thread validation. //! //! # Implementation //! -//! This module uses a monotonic atomic counter to generate unique thread IDs, guaranteeing -//! that each thread receives a distinct non-zero identifier. This approach ensures: +//! This module uses a monotonic atomic counter to generate unique thread IDs, +//! guaranteeing that each thread receives a distinct non-zero identifier. This +//! approach ensures: //! - **Uniqueness**: No hash collisions possible //! - **Efficiency**: Thread-local caching via `thread_local!` //! - **Compatibility**: Works with atomic compare-exchange operations @@ -48,8 +49,9 @@ thread_local! { /// Returns a unique numeric identifier for the current thread. /// -/// This function is thread-safe and returns the same ID when called multiple times -/// from the same thread. Each thread is guaranteed to receive a distinct non-zero ID. +/// This function is thread-safe and returns the same ID when called multiple +/// times from the same thread. Each thread is guaranteed to receive a distinct +/// non-zero ID. /// /// # Returns /// @@ -68,17 +70,21 @@ thread_local! { /// # Performance /// /// This function is very fast as it simply reads from thread-local storage -/// after the first call. The initial call per thread performs one atomic increment. +/// after the first call. The initial call per thread performs one atomic +/// increment. pub fn get_current_thread_id() -> NonZero { CURRENT_THREAD_ID.with(|id| *id) } #[cfg(test)] mod tests { + use std::{ + collections::HashSet, + sync::{Arc, Mutex}, + thread, + }; + use super::*; - use std::collections::HashSet; - use std::sync::{Arc, Mutex}; - use std::thread; #[test] fn test_same_thread_returns_same_id() { diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..c45153d --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,19 @@ +unstable_features = true + +style_edition = "2024" + +group_imports = "StdExternalCrate" +imports_granularity = "Crate" +reorder_imports = true + +wrap_comments = true +normalize_comments = true + +reorder_impl_items = true +condense_wildcard_suffixes = true +enum_discrim_align_threshold = 20 +use_field_init_shorthand = true + +format_strings = true +format_code_in_doc_comments = true +format_macro_matchers = true From ec39dd88cdb46b79de43445836013ac305635fe8 Mon Sep 17 00:00:00 2001 From: Fantix King Date: Sat, 11 Apr 2026 10:39:25 -0400 Subject: [PATCH 2/4] refactor: rename socket to net mod --- compio-py/src/event_loop.rs | 5 ++--- compio-py/src/lib.rs | 4 ++-- compio-py/src/{socket.rs => net/mod.rs} | 0 compio-py/src/ssl.rs | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) rename compio-py/src/{socket.rs => net/mod.rs} (100%) diff --git a/compio-py/src/event_loop.rs b/compio-py/src/event_loop.rs index eb03a98..2d06475 100644 --- a/compio-py/src/event_loop.rs +++ b/compio-py/src/event_loop.rs @@ -21,10 +21,9 @@ use pyo3::{ use crate::{ handle::{Handle, TimerHandle}, - import, + import, net, owned::{self, OwnedRefCell}, runtime::{self, Runtime}, - socket, }; static COMPIO_FUTURE: OnceCell> = OnceCell::new(); @@ -267,7 +266,7 @@ impl CompioLoop { }; let pyloop = slf.clone().unbind(); slf.borrow() - .spawn_py(py, socket::PySocket::new(pyloop, domain, ty, protocol)) + .spawn_py(py, net::PySocket::new(pyloop, domain, ty, protocol)) } } diff --git a/compio-py/src/lib.rs b/compio-py/src/lib.rs index ffc72e9..96a9d38 100644 --- a/compio-py/src/lib.rs +++ b/compio-py/src/lib.rs @@ -9,10 +9,10 @@ use pyo3::{buffer::PyBuffer, prelude::*}; mod event_loop; mod handle; mod import; +mod net; mod owned; mod runtime; mod send_wrapper; -mod socket; mod ssl; mod thread; @@ -38,7 +38,7 @@ fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> { event_loop::register(m)?; handle::register(m)?; - socket::register(m)?; + net::register(m)?; ssl::register(m)?; Ok(()) } diff --git a/compio-py/src/socket.rs b/compio-py/src/net/mod.rs similarity index 100% rename from compio-py/src/socket.rs rename to compio-py/src/net/mod.rs diff --git a/compio-py/src/ssl.rs b/compio-py/src/ssl.rs index f9424a3..31e51ff 100644 --- a/compio-py/src/ssl.rs +++ b/compio-py/src/ssl.rs @@ -42,7 +42,7 @@ use webpki::{ UnknownStatusPolicy, }; -use crate::{Either, event_loop::CompioLoop, import, py_any_to_buffer, socket::SocketStream}; +use crate::{Either, event_loop::CompioLoop, import, net::SocketStream, py_any_to_buffer}; #[derive(Debug, Clone, Copy, Default)] pub enum SSLImpl { From a05c0ee25ef393b6e12652cc506dd611ec119089 Mon Sep 17 00:00:00 2001 From: Fantix King Date: Sat, 11 Apr 2026 10:53:28 -0400 Subject: [PATCH 3/4] refactor: split PySocket into net::socket --- compio-py/src/net/mod.rs | 314 +--------------------------------- compio-py/src/net/socket.rs | 327 ++++++++++++++++++++++++++++++++++++ 2 files changed, 334 insertions(+), 307 deletions(-) create mode 100644 compio-py/src/net/socket.rs diff --git a/compio-py/src/net/mod.rs b/compio-py/src/net/mod.rs index 2adf77b..ea14116 100644 --- a/compio-py/src/net/mod.rs +++ b/compio-py/src/net/mod.rs @@ -1,330 +1,30 @@ // SPDX-License-Identifier: Apache-2.0 OR MulanPSL-2.0 // Copyright 2026 Fantix King -use std::{ - io, - net::{Ipv4Addr, Shutdown, SocketAddr, SocketAddrV4}, - sync::Arc, -}; +use std::{io, net::Shutdown}; use compio::{ buf::{BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut, buf_try}, driver::{ - AsRawFd, ToSharedFd, impl_raw_fd, + ToSharedFd, impl_raw_fd, op::{self, BufResultExt}, }, io::{AsyncRead, AsyncWrite}, - tls::{ - TlsAcceptor, TlsConnector, - py_dynamic_openssl::{self, SSLContext}, - rustls::{self, pki_types::CertificateDer}, - }, }; use pyo3::{ - IntoPyObjectExt, - exceptions::{PyOSError, PyTypeError, PyValueError}, + exceptions::PyTypeError, prelude::*, types::{PyByteArray, PyBytes, PyList, PyTuple}, }; use socket2::{Domain, Protocol, SockAddr, Socket as Socket2, Type}; +pub use self::socket::PySocket; use crate::{ - Either, - event_loop::CompioLoop, - extract_py_err, import, py_any_to_buffer, + import, runtime::{self, Attacher}, - ssl::{RustlsContext, SSLImpl, SSLSocket, SSLSocketMetadata}, }; -#[pyclass(unsendable, name = "Socket")] -pub struct PySocket { - pyloop: Py, - domain: Domain, - ty: Type, - protocol: Option, - inner: Option, - bound: bool, -} - -impl PySocket { - pub async fn new( - pyloop: Py, - domain: Domain, - ty: Type, - protocol: Option, - ) -> PyResult> { - let inner = Some(Socket::new(domain, ty, protocol).await?); - Python::attach(|py| { - Bound::new( - py, - Self { - pyloop, - domain, - ty, - protocol, - inner, - bound: false, - }, - )? - .into_py_any(py) - }) - } - - #[inline] - fn inner(&self) -> PyResult<&Socket> { - self.inner - .as_ref() - .ok_or_else(|| PyOSError::new_err("socket is closed")) - } -} - -#[pymethods] -impl PySocket { - fn __repr__(&self) -> PyResult { - if let Some(inner) = &self.inner { - let fd = inner.as_raw_fd(); - let family = i32::from(self.domain); - let ty = i32::from(self.ty); - let proto = self.protocol.map(i32::from).unwrap_or_default(); - let laddr = match inner.socket.local_addr() { - Ok(addr) => match addr.as_socket() { - Some(addr) => format!(", laddr={addr}"), - None => unimplemented!("unix socket"), - }, - Err(_) => "".to_string(), - }; - Ok(format!( - "" - )) - } else { - Ok("".to_string()) - } - } - - #[pyo3(signature = (address, /))] - fn connect<'py>( - slf: &Bound<'py, Self>, - py: Python<'py>, - address: Py, - ) -> PyResult> { - let this = slf.clone().unbind(); - let slf = slf.borrow().pyloop.bind(py).borrow(); - slf.spawn_py(py, async move { - let (domain, inner) = Python::attach(|py| { - let this = this.bind(py).borrow(); - this.inner().cloned().map(|inner| (this.domain, inner)) - })?; - match domain { - Domain::IPV4 => { - let (result, port) = Python::attach(|py| { - let (host, port): (Bound, u16) = address.extract(py)?; - idna_converter(&host, |name| { - // https://github.com/rust-lang/rust/issues/101035 - match str::from_utf8(name)?.parse::() { - Ok(ip) => Ok((Ok(ip), port)), - Err(_) => Ok((Err(name.to_owned()), port)), - } - }) - })?; - let ip = match result { - Ok(ip) => ip, - Err(name) => name_to_ip(name, domain).await?.parse()?, - }; - if cfg!(windows) && !Python::attach(|py| this.bind(py).borrow().bound) { - inner - .socket - .bind(&SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0).into())?; - Python::attach(|py| this.bind(py).borrow_mut().bound = true); - } - let addr = SocketAddr::new(ip.into(), port).into(); - inner.connect_async(&addr).await?; - } - _ => unimplemented!(), - }; - Python::attach(|py| { - this.bind(py).borrow_mut().bound = true; - py.None().into_py_any(py) - }) - }) - } - - #[pyo3(signature = (bufsize, flags = 0, /))] - fn recv<'py>( - slf: &Bound, - py: Python<'py>, - bufsize: usize, - flags: i32, - ) -> PyResult> { - let this = slf.clone().unbind(); - let slf = slf.borrow().pyloop.bind(py).borrow(); - slf.spawn_py(py, async move { - let inner = Python::attach(|py| this.bind(py).borrow().inner().cloned())?; - let buf: Vec = Vec::with_capacity(bufsize); - let (bytes_read, buf) = buf_try!(@try inner.recv(buf, flags).await); - Python::attach(|py| { - PyBytes::new_with_writer(py, bytes_read, |w| Ok(w.write_all(&buf[..bytes_read])?))? - .into_py_any(py) - }) - }) - } - - #[pyo3(signature = (data, flags = 0, /))] - fn send<'py>( - slf: &Bound, - py: Python<'py>, - data: Py, - flags: i32, - ) -> PyResult> { - let this = slf.clone().unbind(); - let slf = slf.borrow().pyloop.bind(py).borrow(); - slf.spawn_py(py, async move { - let (inner, buf) = Python::attach(|py| { - let inner = this.bind(py).borrow().inner()?.clone(); - py_any_to_buffer(py, data.bind(py)).map(|buf| (inner, buf)) - })?; - let (bytes_written, _) = buf_try!(@try inner.send(buf, flags).await); - drop(data); - Python::attach(|py| bytes_written.into_py_any(py)) - }) - } - - #[pyo3(signature = (sslcontext=None, *, server_side=false, server_hostname=None))] - fn start_tls<'py>( - slf: &Bound, - py: Python<'py>, - sslcontext: Option>, - server_side: bool, - server_hostname: Option, - ) -> PyResult> { - let this = slf.clone().unbind(); - let slf = slf.borrow().pyloop.bind(py).borrow(); - slf.spawn_py(py, async move { - let mut metadata = SSLSocketMetadata::default(); - metadata.server_side = server_side; - - // First, verify parameters and prepare either TlsAcceptor or TlsConnector. - let tls = Python::attach(|py| { - let has_ossl = py_dynamic_openssl::load_py(py)?; - // Coerce sslcontext to either SSLContext or RustlsContext - let ctx: Either<_, Bound> = match sslcontext - .map(|ctx| ctx.into_bound(py)) - { - Some(ctx) if import::ssl::is_ssl_context(py, &ctx)? => has_ossl - .then(|| Either::Left(ctx)) - .ok_or_else(|| PyTypeError::new_err("ssl.SSLContext is not supported"))?, - Some(ctx) => ctx.cast_into().map(Either::Right).map_err(|e| { - PyTypeError::new_err(format!("illegal sslcontext: {:?}", e.into_inner())) - })?, - None if !server_side => Either::Left(import::ssl::create_default_context(py)?), - None => Err(PyValueError::new_err("server_side requires sslcontext"))?, - }; - // Build TlsAcceptor or TlsConnector from the context - match ctx { - Either::Left(ctx) if has_ossl => SSLContext::try_from(ctx).map(|ctx| { - if server_side { - Either::Left(TlsAcceptor::from(ctx)) - } else { - Either::Right(TlsConnector::from(ctx)) - } - }), - Either::Left(ctx) => { - // This case is guaranteed to be a default client-side SSLContext - debug_assert!(!server_side); - metadata.implementation = SSLImpl::Rustls; - let ca_certs: Bound = - ctx.call_method1("get_ca_certs", (true,))?.cast_into()?; - let mut root_store = rustls::RootCertStore::empty(); - for cert in ca_certs.iter() { - let cert: Bound = cert.cast_into()?; - let cert = cert.as_bytes(); - root_store - .add(CertificateDer::from(cert)) - .map_err(|e| PyValueError::new_err(e.to_string()))?; - } - let config = rustls::ClientConfig::builder() - .with_root_certificates(root_store) - .with_no_client_auth(); - Ok(Either::Right(TlsConnector::from(Arc::new(config)))) - } - Either::Right(ctx) => { - metadata.implementation = SSLImpl::Rustls; - let ctx = ctx.borrow(); - Ok(match ctx.build(py, server_side)? { - Either::Left(c) => Either::Left(TlsAcceptor::from(c)), - Either::Right(c) => Either::Right(TlsConnector::from(c)), - }) - } - } - })?; - - // Then, do TLS handshake accordingly - let Some(inner) = Python::attach(|py| this.bind(py).borrow_mut().inner.take()) else { - return Err(PyOSError::new_err("socket is closed")); - }; - metadata.fd = inner.as_raw_fd(); - let stream = SocketStream { inner }; - let stream = match tls { - Either::Left(acceptor) => extract_py_err(acceptor.accept(stream).await)?, - Either::Right(connector) => { - let name = match server_hostname { - Some(name) => name, - None => stream - .inner - .socket - .peer_addr()? - .as_socket() - .ok_or_else(|| PyValueError::new_err("unknown server_hostname"))? - .ip() - .to_string(), - }; - extract_py_err(connector.connect(&name, stream).await)? - } - }; - - // At last, wrap the TlsStream in an SSLSocket - Python::attach(|py| { - let this = this.bind(py).borrow(); - SSLSocket::new(py, &this.pyloop, stream, metadata)?.into_py_any(py) - }) - }) - } - - #[pyo3(signature = (how, /))] - fn shutdown<'py>(slf: &Bound, py: Python<'py>, how: i32) -> PyResult> { - let this = slf.clone().unbind(); - let slf = slf.borrow().pyloop.bind(py).borrow(); - slf.spawn_py(py, async move { - let (inner, how) = Python::attach(|py| { - let inner = this.bind(py).borrow().inner()?.clone(); - let how = if how == import::socket::shut_wr(py)? { - Shutdown::Write - } else if how == import::socket::shut_rd(py)? { - Shutdown::Read - } else if how == import::socket::shut_rdwr(py)? { - Shutdown::Both - } else { - return Err(PyValueError::new_err(format!( - "invalid shutdown how: {how}" - ))); - }; - Ok((inner, how)) - })?; - inner.shutdown(how).await?; - Python::attach(|py| py.None().into_py_any(py)) - }) - } - - fn close<'py>(slf: &Bound<'py, Self>, py: Python<'py>) -> PyResult> { - let this = slf.clone().unbind(); - let slf = slf.borrow().pyloop.bind(py).borrow(); - slf.spawn_py(py, async move { - if let Some(inner) = Python::attach(|py| this.bind(py).borrow_mut().inner.take()) { - inner.close().await?; - }; - Python::attach(|py| py.None().into_py_any(py)) - }) - } -} +mod socket; #[derive(Debug, Clone)] pub struct SocketStream { @@ -421,7 +121,7 @@ where } pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; + socket::register(m)?; Ok(()) } diff --git a/compio-py/src/net/socket.rs b/compio-py/src/net/socket.rs new file mode 100644 index 0000000..88ae0d1 --- /dev/null +++ b/compio-py/src/net/socket.rs @@ -0,0 +1,327 @@ +// SPDX-License-Identifier: Apache-2.0 OR MulanPSL-2.0 +// Copyright 2026 Fantix King + +use std::{ + net::{Ipv4Addr, Shutdown, SocketAddr, SocketAddrV4}, + sync::Arc, +}; + +use compio::{ + buf::buf_try, + driver::AsRawFd, + tls::{ + TlsAcceptor, TlsConnector, + py_dynamic_openssl::{self, SSLContext}, + rustls::{self, pki_types::CertificateDer}, + }, +}; +use pyo3::{ + IntoPyObjectExt, + exceptions::{PyOSError, PyTypeError, PyValueError}, + prelude::*, + types::{PyBytes, PyList}, +}; +use socket2::{Domain, Protocol, Type}; + +use super::{Socket, SocketStream, idna_converter, name_to_ip}; +use crate::{ + Either, + event_loop::CompioLoop, + extract_py_err, import, py_any_to_buffer, + ssl::{RustlsContext, SSLImpl, SSLSocket, SSLSocketMetadata}, +}; + +#[pyclass(unsendable, name = "Socket")] +pub struct PySocket { + pyloop: Py, + domain: Domain, + ty: Type, + protocol: Option, + inner: Option, + bound: bool, +} + +impl PySocket { + pub async fn new( + pyloop: Py, + domain: Domain, + ty: Type, + protocol: Option, + ) -> PyResult> { + let inner = Some(Socket::new(domain, ty, protocol).await?); + Python::attach(|py| { + Bound::new( + py, + Self { + pyloop, + domain, + ty, + protocol, + inner, + bound: false, + }, + )? + .into_py_any(py) + }) + } + + #[inline] + fn inner(&self) -> PyResult<&Socket> { + self.inner + .as_ref() + .ok_or_else(|| PyOSError::new_err("socket is closed")) + } +} + +#[pymethods] +impl PySocket { + fn __repr__(&self) -> PyResult { + if let Some(inner) = &self.inner { + let fd = inner.as_raw_fd(); + let family = i32::from(self.domain); + let ty = i32::from(self.ty); + let proto = self.protocol.map(i32::from).unwrap_or_default(); + let laddr = match inner.socket.local_addr() { + Ok(addr) => match addr.as_socket() { + Some(addr) => format!(", laddr={addr}"), + None => unimplemented!("unix socket"), + }, + Err(_) => "".to_string(), + }; + Ok(format!( + "" + )) + } else { + Ok("".to_string()) + } + } + + #[pyo3(signature = (address, /))] + fn connect<'py>( + slf: &Bound<'py, Self>, + py: Python<'py>, + address: Py, + ) -> PyResult> { + let this = slf.clone().unbind(); + let slf = slf.borrow().pyloop.bind(py).borrow(); + slf.spawn_py(py, async move { + let (domain, inner) = Python::attach(|py| { + let this = this.bind(py).borrow(); + this.inner().cloned().map(|inner| (this.domain, inner)) + })?; + match domain { + Domain::IPV4 => { + let (result, port) = Python::attach(|py| { + let (host, port): (Bound, u16) = address.extract(py)?; + idna_converter(&host, |name| { + // https://github.com/rust-lang/rust/issues/101035 + match str::from_utf8(name)?.parse::() { + Ok(ip) => Ok((Ok(ip), port)), + Err(_) => Ok((Err(name.to_owned()), port)), + } + }) + })?; + let ip = match result { + Ok(ip) => ip, + Err(name) => name_to_ip(name, domain).await?.parse()?, + }; + if cfg!(windows) && !Python::attach(|py| this.bind(py).borrow().bound) { + inner + .socket + .bind(&SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0).into())?; + Python::attach(|py| this.bind(py).borrow_mut().bound = true); + } + let addr = SocketAddr::new(ip.into(), port).into(); + inner.connect_async(&addr).await?; + } + _ => unimplemented!(), + }; + Python::attach(|py| { + this.bind(py).borrow_mut().bound = true; + py.None().into_py_any(py) + }) + }) + } + + #[pyo3(signature = (bufsize, flags = 0, /))] + fn recv<'py>( + slf: &Bound, + py: Python<'py>, + bufsize: usize, + flags: i32, + ) -> PyResult> { + let this = slf.clone().unbind(); + let slf = slf.borrow().pyloop.bind(py).borrow(); + slf.spawn_py(py, async move { + let inner = Python::attach(|py| this.bind(py).borrow().inner().cloned())?; + let buf: Vec = Vec::with_capacity(bufsize); + let (bytes_read, buf) = buf_try!(@try inner.recv(buf, flags).await); + Python::attach(|py| { + PyBytes::new_with_writer(py, bytes_read, |w| Ok(w.write_all(&buf[..bytes_read])?))? + .into_py_any(py) + }) + }) + } + + #[pyo3(signature = (data, flags = 0, /))] + fn send<'py>( + slf: &Bound, + py: Python<'py>, + data: Py, + flags: i32, + ) -> PyResult> { + let this = slf.clone().unbind(); + let slf = slf.borrow().pyloop.bind(py).borrow(); + slf.spawn_py(py, async move { + let (inner, buf) = Python::attach(|py| { + let inner = this.bind(py).borrow().inner()?.clone(); + py_any_to_buffer(py, data.bind(py)).map(|buf| (inner, buf)) + })?; + let (bytes_written, _) = buf_try!(@try inner.send(buf, flags).await); + drop(data); + Python::attach(|py| bytes_written.into_py_any(py)) + }) + } + + #[pyo3(signature = (sslcontext=None, *, server_side=false, server_hostname=None))] + fn start_tls<'py>( + slf: &Bound, + py: Python<'py>, + sslcontext: Option>, + server_side: bool, + server_hostname: Option, + ) -> PyResult> { + let this = slf.clone().unbind(); + let slf = slf.borrow().pyloop.bind(py).borrow(); + slf.spawn_py(py, async move { + let mut metadata = SSLSocketMetadata::default(); + metadata.server_side = server_side; + + // First, verify parameters and prepare either TlsAcceptor or TlsConnector. + let tls = Python::attach(|py| { + let has_ossl = py_dynamic_openssl::load_py(py)?; + // Coerce sslcontext to either SSLContext or RustlsContext + let ctx: Either<_, Bound> = match sslcontext + .map(|ctx| ctx.into_bound(py)) + { + Some(ctx) if import::ssl::is_ssl_context(py, &ctx)? => has_ossl + .then(|| Either::Left(ctx)) + .ok_or_else(|| PyTypeError::new_err("ssl.SSLContext is not supported"))?, + Some(ctx) => ctx.cast_into().map(Either::Right).map_err(|e| { + PyTypeError::new_err(format!("illegal sslcontext: {:?}", e.into_inner())) + })?, + None if !server_side => Either::Left(import::ssl::create_default_context(py)?), + None => Err(PyValueError::new_err("server_side requires sslcontext"))?, + }; + // Build TlsAcceptor or TlsConnector from the context + match ctx { + Either::Left(ctx) if has_ossl => SSLContext::try_from(ctx).map(|ctx| { + if server_side { + Either::Left(TlsAcceptor::from(ctx)) + } else { + Either::Right(TlsConnector::from(ctx)) + } + }), + Either::Left(ctx) => { + // This case is guaranteed to be a default client-side SSLContext + debug_assert!(!server_side); + metadata.implementation = SSLImpl::Rustls; + let ca_certs: Bound = + ctx.call_method1("get_ca_certs", (true,))?.cast_into()?; + let mut root_store = rustls::RootCertStore::empty(); + for cert in ca_certs.iter() { + let cert: Bound = cert.cast_into()?; + let cert = cert.as_bytes(); + root_store + .add(CertificateDer::from(cert)) + .map_err(|e| PyValueError::new_err(e.to_string()))?; + } + let config = rustls::ClientConfig::builder() + .with_root_certificates(root_store) + .with_no_client_auth(); + Ok(Either::Right(TlsConnector::from(Arc::new(config)))) + } + Either::Right(ctx) => { + metadata.implementation = SSLImpl::Rustls; + let ctx = ctx.borrow(); + Ok(match ctx.build(py, server_side)? { + Either::Left(c) => Either::Left(TlsAcceptor::from(c)), + Either::Right(c) => Either::Right(TlsConnector::from(c)), + }) + } + } + })?; + + // Then, do TLS handshake accordingly + let Some(inner) = Python::attach(|py| this.bind(py).borrow_mut().inner.take()) else { + return Err(PyOSError::new_err("socket is closed")); + }; + metadata.fd = inner.as_raw_fd(); + let stream = SocketStream { inner }; + let stream = match tls { + Either::Left(acceptor) => extract_py_err(acceptor.accept(stream).await)?, + Either::Right(connector) => { + let name = match server_hostname { + Some(name) => name, + None => stream + .inner + .socket + .peer_addr()? + .as_socket() + .ok_or_else(|| PyValueError::new_err("unknown server_hostname"))? + .ip() + .to_string(), + }; + extract_py_err(connector.connect(&name, stream).await)? + } + }; + + // At last, wrap the TlsStream in an SSLSocket + Python::attach(|py| { + let this = this.bind(py).borrow(); + SSLSocket::new(py, &this.pyloop, stream, metadata)?.into_py_any(py) + }) + }) + } + + #[pyo3(signature = (how, /))] + fn shutdown<'py>(slf: &Bound, py: Python<'py>, how: i32) -> PyResult> { + let this = slf.clone().unbind(); + let slf = slf.borrow().pyloop.bind(py).borrow(); + slf.spawn_py(py, async move { + let (inner, how) = Python::attach(|py| { + let inner = this.bind(py).borrow().inner()?.clone(); + let how = if how == import::socket::shut_wr(py)? { + Shutdown::Write + } else if how == import::socket::shut_rd(py)? { + Shutdown::Read + } else if how == import::socket::shut_rdwr(py)? { + Shutdown::Both + } else { + return Err(PyValueError::new_err(format!( + "invalid shutdown how: {how}" + ))); + }; + Ok((inner, how)) + })?; + inner.shutdown(how).await?; + Python::attach(|py| py.None().into_py_any(py)) + }) + } + + fn close<'py>(slf: &Bound<'py, Self>, py: Python<'py>) -> PyResult> { + let this = slf.clone().unbind(); + let slf = slf.borrow().pyloop.bind(py).borrow(); + slf.spawn_py(py, async move { + if let Some(inner) = Python::attach(|py| this.bind(py).borrow_mut().inner.take()) { + inner.close().await?; + }; + Python::attach(|py| py.None().into_py_any(py)) + }) + } +} + +pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + Ok(()) +} From edc00507314ec6153543d1311010bdaab4eba65c Mon Sep 17 00:00:00 2001 From: Fantix King Date: Sat, 11 Apr 2026 10:57:12 -0400 Subject: [PATCH 4/4] refactor: move ssl to net::ssl --- compio-py/src/lib.rs | 2 -- compio-py/src/net/mod.rs | 2 ++ compio-py/src/net/socket.rs | 8 +++----- compio-py/src/{ => net}/ssl.rs | 0 4 files changed, 5 insertions(+), 7 deletions(-) rename compio-py/src/{ => net}/ssl.rs (100%) diff --git a/compio-py/src/lib.rs b/compio-py/src/lib.rs index 96a9d38..fd29ef1 100644 --- a/compio-py/src/lib.rs +++ b/compio-py/src/lib.rs @@ -13,7 +13,6 @@ mod net; mod owned; mod runtime; mod send_wrapper; -mod ssl; mod thread; /// A Python module implemented in Rust. The name of this module must match @@ -39,7 +38,6 @@ fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> { event_loop::register(m)?; handle::register(m)?; net::register(m)?; - ssl::register(m)?; Ok(()) } diff --git a/compio-py/src/net/mod.rs b/compio-py/src/net/mod.rs index ea14116..fae8eb0 100644 --- a/compio-py/src/net/mod.rs +++ b/compio-py/src/net/mod.rs @@ -25,6 +25,7 @@ use crate::{ }; mod socket; +mod ssl; #[derive(Debug, Clone)] pub struct SocketStream { @@ -122,6 +123,7 @@ where pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { socket::register(m)?; + ssl::register(m)?; Ok(()) } diff --git a/compio-py/src/net/socket.rs b/compio-py/src/net/socket.rs index 88ae0d1..14776e5 100644 --- a/compio-py/src/net/socket.rs +++ b/compio-py/src/net/socket.rs @@ -23,13 +23,11 @@ use pyo3::{ }; use socket2::{Domain, Protocol, Type}; -use super::{Socket, SocketStream, idna_converter, name_to_ip}; -use crate::{ - Either, - event_loop::CompioLoop, - extract_py_err, import, py_any_to_buffer, +use super::{ + Socket, SocketStream, idna_converter, name_to_ip, ssl::{RustlsContext, SSLImpl, SSLSocket, SSLSocketMetadata}, }; +use crate::{Either, event_loop::CompioLoop, extract_py_err, import, py_any_to_buffer}; #[pyclass(unsendable, name = "Socket")] pub struct PySocket { diff --git a/compio-py/src/ssl.rs b/compio-py/src/net/ssl.rs similarity index 100% rename from compio-py/src/ssl.rs rename to compio-py/src/net/ssl.rs