From 86fcc80b348a3f00ab81e5447924aa10202d95e8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 29 May 2026 03:42:14 +0900 Subject: refactor(repository): introduce typed LockArrayRepositoryHandle Share LockArrayRepository via Rc> while preserving the PHP ?LockArrayRepository type strength: Request.locked_repository, Locker ::get_locked_repository, and the installer create_* helpers now thread the typed handle instead of an owned LockArrayRepository, and pool builder identity check becomes a strict ptr_eq via widening into RepositoryInterfaceHandle. Co-Authored-By: Claude Opus 4.7 (1M context) --- crates/shirabe/src/repository/handle.rs | 63 +++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) (limited to 'crates/shirabe/src/repository/handle.rs') diff --git a/crates/shirabe/src/repository/handle.rs b/crates/shirabe/src/repository/handle.rs index f7a4104..a4225f7 100644 --- a/crates/shirabe/src/repository/handle.rs +++ b/crates/shirabe/src/repository/handle.rs @@ -10,8 +10,8 @@ use shirabe_semver::constraint::AnyConstraint; use crate::package::BasePackageHandle; use crate::package::PackageInterfaceHandle; use crate::repository::{ - FindPackageConstraint, InstalledRepositoryInterface, LoadPackagesResult, ProviderInfo, - RepositoryInterface, SearchResult, WritableRepositoryInterface, + FindPackageConstraint, InstalledRepositoryInterface, LoadPackagesResult, LockArrayRepository, + ProviderInfo, RepositoryInterface, SearchResult, WritableRepositoryInterface, }; /// Shared reference to a repository. Corresponds to PHP `RepositoryInterface`. @@ -172,3 +172,62 @@ impl std::hash::Hash for RepositoryInterfaceHandle { self.ptr_id().hash(state); } } + +/// Typed shared handle over `LockArrayRepository`. Preserves the PHP `?LockArrayRepository` +/// typing where a `RepositoryInterfaceHandle` would be too wide. +#[derive(Debug, Clone)] +pub struct LockArrayRepositoryHandle(Rc>); + +impl LockArrayRepositoryHandle { + pub fn new(repository: LockArrayRepository) -> Self { + let rc: Rc> = Rc::new(RefCell::new(repository)); + let rc_dyn: Rc> = rc.clone(); + rc.borrow().set_self_handle(Rc::downgrade(&rc_dyn)); + Self(rc) + } + + pub fn from_rc(rc: Rc>) -> Self { + Self(rc) + } + + pub fn as_rc(&self) -> &Rc> { + &self.0 + } + + pub fn borrow(&self) -> Ref<'_, LockArrayRepository> { + self.0.borrow() + } + + pub fn borrow_mut(&self) -> RefMut<'_, LockArrayRepository> { + self.0.borrow_mut() + } + + pub fn ptr_eq(&self, other: &Self) -> bool { + Rc::ptr_eq(&self.0, &other.0) + } + + pub fn ptr_id(&self) -> usize { + Rc::as_ptr(&self.0) as *const () as usize + } +} + +impl From for RepositoryInterfaceHandle { + fn from(h: LockArrayRepositoryHandle) -> Self { + let rc: Rc> = h.0; + RepositoryInterfaceHandle::from_rc(rc) + } +} + +impl PartialEq for LockArrayRepositoryHandle { + fn eq(&self, other: &Self) -> bool { + Rc::ptr_eq(&self.0, &other.0) + } +} + +impl Eq for LockArrayRepositoryHandle {} + +impl std::hash::Hash for LockArrayRepositoryHandle { + fn hash(&self, state: &mut H) { + self.ptr_id().hash(state); + } +} -- cgit v1.3.1