Repository navigation
feat: implement MappedMutexGuard - #61
Conversation
…d' into feat/implement_mapped_mutex_guard
Both sound reasonable. I just don't have time to implement time before :D And yes let's keep this further improvement in a follow-up PR. |
|
Thanks for your contribution @orthur2! Very detailed description and neat changes. Comments above. |
tisonkun
left a comment
There was a problem hiding this comment.
LGTM. I push a new commit to updates some coments. Rest is great.
|
Thanks for your contribution!
If you're interested in these, welcome to drop new patches. And you can ping me as a reviewer. |
Thanks a lot for the review and for handling the comments.Glad I could contribute! |
Well, I would like to continue contributing when I have some free time. |
First and foremost, thank you for assigning this issue to me. Here is the work for review.
This PR implements the six variants of mapped guards for both
MutexandRwLock, as proposed in issue #59:MappedMutexGuard(NonOwned, Mutex)OwnedMappedMutexGuard(Owned, Mutex)MappedRwLockReadGuard(NonOwned, RWLockRead)OwnedMappedRwLockReadGuard(Owned, RWLockRead)MappedRwLockWriteGuard(NonOwned, RWLockWrite)OwnedMappedRwLockWriteGuard(Owned, RWLockWrite)The difficult parts of the programming process were the strenuous derivation of the
SendandSynctraits, as well as managing lifetimes.The current implementation may have some issues. Please feel free to review the code thoroughly and point out any problems or even the smallest errors. I would be very grateful for your feedback.
It's worth noting that I hesitated for a long time on whether to use
try_map()orfilter_map(), even though I saw that @tisonkun's PR #58 usedtry_map(). In my view, the name has a significant impact. Whilefilter_map()is a well-regarded naming choice, and in fact, many designs in the Rust community have evolved fromtry_map()tofilter_map(), I ultimately chose to usetry_map. My reasoning is that from a user's perspective, the most critical piece of information is that this function might fail and they need to handle itsResultreturn value. The namefilter_mapfocuses more on the function's internal mechanism, whereastry_mapbetter communicates the operation's external behavior.Another naming challenge I faced was with the "owned" guards. Although I initially listed them as
OwnedMappedMutexGuard,OwnedMappedRwLockWriteGuard, andOwnedMappedRwLockReadGuardwhen I took the issue. However, during development, I was torn betweenOwned(MappedMutexGuard)andMapped(OwnedMutexGuard). If we follow a functional approach ofOperation(Object), thenMapped(OwnedMutexGuard)would be the name that best reflects the process. However, I ultimately choseOwnedMappedMutexGuardbecause I feel it better describes the properties of the Mapped....Guardstructitself. (Owned or NonOwned)If you have different ideas, please share your views.
What I should mention that I do else:
I moved the
read(),try_read(),write(),try_write(),read_owned(),write_owned(),try_read_owned(), andtry_write_owned()methods of theRwLock<T>struct from their individual files (read_guard.rs,write_guard.rs, etc.) intomod.rs. I believe it's better to have the method implementations (impl TheType { ... }) in the same file as the type definition (struct TheType { ... }). All these methods belong with theRwLockstruct definition.I fixed a documentation build warning in the
mpscmodule (unresolved link to UnboundedSender::send,unresolved link toBoundedSender::send, etc.). This was a minor, unrelated warning, but with my preference for keeping things tidy, I fixed it here. I apologize for forgeting creating a separate PR for this, as it is technically out of scope for this issue. Since it was a small change, hope you don't mind me adding it here.Some related work recommendations for the future:
MappedRwLockWriteGuard. However, I noticed the existingWriteGuarddidn't have a similar function, so I omitted it. This would be better suited for a separate feature request. After a quick search, I found that both the Rust standard library and Tokio providedowngrade()and eventry_downgrade_map.mutex/mod.rsfile into separate files likemutexguard.rs,ownedmutexguard.rs, etc., similar to the structure of therwlockmodule. I was originally going to do this while working on the issue but decided against it for this PR to avoid extensive code changes that would complicate the review process.Closes #59