-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Manual block encodings #636
Conversation
Right, I think my preferred solution (for now) would be something like the following instead (similar to your proposal in #442 (comment), just with an extra trait): unsafe trait BlockEncodingHelper { // Bikeshed name
type Arguments: EncodingArguments;
type Return: EncodingReturn;
const ENCODING_CSTR: &'static CStr;
}
impl Block {
pub fn with_encoding<'f, A, R, Closure, Helper>(
_helper: Helper,
closure: Closure,
) -> Self
where
Helper: BlockEncodingHelper<Arguments = A, Return = R>,
// ...
{ ... }
}
// Usage
struct Helper;
impl BlockEncodingHelper for Helper {
type Arguments = (i32, Bool);
type Return = u8;
const ENCODING_CSTR: &'static CStr = c"whatever_the_encoding_is_I_have_not_checked_it";
}
let block = RcBlock::with_encoding(Helper, |arg1, arg2| {
if arg2.as_bool() {
arg1 as u8
} else {
0
}
}); |
Ah yes, I was suspecting something of the sort was happening behind the scenes, but did not know the exact underlying mechanism. Thanks for the link.
That seems like it could work, yes thanks. I'll try it soon and see if the promotion works in this case too. |
f796013
to
b7a47cf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't reviewed all of it yet (use GitHub's UI to request a review when you want me to do so), only saw that you're bumping MSRV, please don't do that.
I think all the examples and tests can use c"string"
without affecting MSRV, but in the places where the actual code is, you'll have to use unsafe { CStr::from_bytes_with_nul_unchecked(b"string\0") }
instead.
Yes, sorry for the notifications, I just wanted to make things work and then check that I haven't missed anything, such as adding new tests, hence why I left is as a draft.
That was also part of the things I wanted to do before formally asking for a review, sorry. 😅
|
Preeetty sure that was only
Perhaps we can just avoid using the If not, then there are ways to do the equivalent of inline |
Woops, sorry. That seems right, yes. Looking at the methods I use, I should be able to avoid any bump here, hopefully.
Probably, yes. I just wanted to avoid the "let's hope promotion works similarly in previous and future compiler versions" kind of stance, although it should be acceptable 🤞
I'll try the above first and then this if it fails somehow. Thanks for the suggestion. |
b7a47cf
to
bda07cf
Compare
In the end, the MSRV bump was indeed not necessary:
Last thing I need to do before declaring this ready to review: try to add the |
c2d2088
to
092cbcc
Compare
Alright @madsmtm, this should now be ready for a proper first review cycle! Sorry for the many CI runs, though, I just struggled endlessly with minor details. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, I've been away on holidays. I like the overall approach of this, and I really appreciate the thorough documentation you've put into it!
Sorry for the many CI runs, though, I just struggled endlessly with minor details.
Don't worry, I don't pay for any of it - and it's mostly my own fault, I haven't really provided an easy way to test everything locally!
crates/objc2-encode/src/encoding.rs
Outdated
/// | ||
/// [`objc2::encode::block_signature_string`]: https://docs.rs/objc2/latest/objc2/encode/fn.block_signature_string.html | ||
pub fn block_signature_string(args: &[Self], ret: &Self) -> String { | ||
// TODO: alignment? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raising this as a comment on the PR for visibility, I'm not sure what to do about alignment myself.
Hmm, I think perhaps a way to get this PR merged quicker would be to rip out the |
e305576
to
e5f8e29
Compare
@madsmtm All the items should be addressed now. The only remaining issue is the MSRV bump requirement. |
Thanks! I'll get back to this, and will fix any nits I have myself, after I've done the MSRV bump. In the odd case that this isn't merged in a week, ping me again ;) |
e5f8e29
to
a6fd511
Compare
Thanks for the MSRV bump. It seems to work without my having to change anything, except for the tests where I use the |
I think it's because even though it's guarded behind You could put it in a separate file and import it with |
a6fd511
to
f6e23d2
Compare
Done 🙂 |
Signed-off-by: Paul Mabileau <[email protected]>
Signed-off-by: Paul Mabileau <[email protected]>
Signed-off-by: Paul Mabileau <[email protected]>
…y-encoded block constructors As per the review: until the block encoding generation is done better. Signed-off-by: Paul Mabileau <[email protected]>
* `StackBlock`'s `Clone` constraint checks: order switch it seems. * `OptionEncode` compile-time checks: small line number shift. Signed-off-by: Paul Mabileau <[email protected]>
Signed-off-by: Paul Mabileau <[email protected]>
Signed-off-by: Paul Mabileau <[email protected]>
f6e23d2
to
2d960ac
Compare
The `ManualBlockEncoding` is already `unsafe`, and contains the block's arguments and return type, so we can use that to mark the actual method that the user calls safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through the whole thing now, thanks for the nice commit sequence! I ended up making with_encoding
safe, since we can more the safety requirement to the ManualBlockEncoding
trait.
And thanks for the excellent documentation and list of examples in ManualBlockEncoding
, could not have written that better!
Cool, thanks for the great collaboration! |
Hey @madsmtm!
This is an attempt to contribute to #442, by adding the ability to manually provide a block encoding to its constructor so that it sets
BLOCK_HAS_SIGNATURE
and saves the string properly.Included:
StackBlock::with_encoding
.RcBlock::with_encoding
.block2::traits::ManualBlockEncoding
is the smuggling trait.cfg(debug_assertions)
: a bit too strict for now, but can be relaxed later on by implementing a dedicated parser and equivalence checking.Cheers,
Paul.