Open
Conversation
kskalski
reviewed
Mar 17, 2026
wincode/src/io/mod.rs
Outdated
| /// | ||
| /// Users of [`Reader`] can call [`Reader::supports_borrow`] to check if a borrow kind | ||
| /// is supported. | ||
| const BORROW_CAP: u8 = 0; |
Contributor
There was a problem hiding this comment.
I don't like _CAP suffix here, because it sounds like a limit (from "hat") or capacity
How about calling it BORROW_SUPPORT, maybe BORROW_KINDS or just a full BORROW_CAPABILITY ?
Also, given that this is mask, could mention it in the name like BORROW_SUPPORT_MASK
Contributor
Author
There was a problem hiding this comment.
Sure -- BORROW_KINDS seems reasonable to me
| /// assert!(!reader.supports_borrow(BorrowKind::Backing)); | ||
| /// ``` | ||
| #[inline] | ||
| fn supports_borrow(&self, kind: BorrowKind) -> bool { |
Contributor
Author
There was a problem hiding this comment.
Unfortunately not -- const functions in traits isn't yet supported. But, I looked at the assembly in a few examples, and the compiler reliably constant-folds conditions out since BORROW_CAP and kind.mask is const
9b63298 to
11b34cc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a constant
BORROW_KINDSto theReadertrait that allows implementations to specify whichBorrowKinds they support. This will allowSchemaReadimplementations to vary their implementation depending on the borrow capabilities of theReader. Conditions based onReader::supports_borrowcan be constant-folded by the compiler, and as such should have no runtime cost.Examples of where this is useful
CowWe don't currently support
Cow, but with this functionality, we can returnCow::BorrowedforReaders that supportBorrowKind::Backing.solana-sdk'sv1::Messagehttps://github.com/anza-xyz/solana-sdk/blob/735c03b78c60d7ae9a98c0cdccac396911881676/message/src/versions/v1/message.rs#L651-L658
If we wanted to make this
SchemaReadimplementation compatible with a broader set ofReaderback-ends in the future, this implementation could heap-allocate theInstructionHeaders if theReaderdoes not supportBorrowKind::Backing.ecowhttps://github.com/tanmay4l/wincode/blob/a08c2349a7e2e8ba835602de5b818c5f6c11f7b1/wincode/src/schema/external/ecow.rs#L36-L44
Similarly, this implementation could check
reader.supports_borrow(BorrowKind::CallSite)rather than matching on the error.