Skip to content

Commit

Permalink
Let c_void be a valid encoding in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jul 31, 2023
1 parent ce6100c commit 2f990df
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/objc2-encode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased - YYYY-MM-DD

### Fixed
* Allow the encoding of `*mut c_void` in a few places where they would not
otherwise be equivalent.


## 2.0.0 - 2023-06-20

Expand Down
21 changes: 21 additions & 0 deletions crates/objc2-encode/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ impl Encoding {
///
/// See [`Encoding::equivalent_to`] for details about the meaning of
/// "equivalence".
///
/// This differs from [`Encoding::equivalent_to`] in the following:
/// - The encoding `Encoding::Pointer(&Encoding::Void)` (`*mut c_void`) is
/// allowed where other types are expected.
pub fn equivalent_to_str(&self, s: &str) -> bool {
let mut parser = Parser::new(s);

Expand Down Expand Up @@ -597,4 +601,21 @@ mod tests {

assert!(!enc.equivalent_to_box(&expected));
}

#[test]
fn pointer_to_void() {
let v = Encoding::Pointer(&Encoding::Void);
let s = Encoding::Pointer(&Encoding::Struct("abc", &[]));

assert!(v.equivalent_to(&s));
assert!(s.equivalent_to(&v));
assert!(v.equivalent_to_str("^{abc=}"));
assert!(s.equivalent_to_str("^v"));

assert!(!Encoding::Atomic(&Encoding::Struct("abc", &[])).equivalent_to(&v));
assert!(!Encoding::Atomic(&Encoding::Void).equivalent_to(&s));

assert!(!Encoding::Void.equivalent_to_str("{abc=}"));
assert!(!Encoding::Struct("abc", &[]).equivalent_to_str("v"));
}
}
15 changes: 14 additions & 1 deletion crates/objc2-encode/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ impl NestingLevel {
Self::Top
}

pub(crate) const fn should_void_allow_non_void(self) -> bool {
match self {
Self::Top => false,
Self::Bottom | Self::Within => true,
}
}

const fn bitfield(self) -> Self {
// This is a bit irrelevant, since bitfields can only contain integral
// types
Expand Down Expand Up @@ -80,9 +87,15 @@ pub(crate) fn compare_encodings<E1: EncodingType, E2: EncodingType>(
level2
};

// TODO: Are level1 and level2 ever be different?
// TODO: Are level1 and level2 ever different?

match (enc1.helper(level1), enc2.helper(level2)) {
(Primitive(crate::helper::Primitive::Void), _) if level1.should_void_allow_non_void() => {
true
}
(_, Primitive(crate::helper::Primitive::Void)) if level2.should_void_allow_non_void() => {
true
}
(Primitive(p1), Primitive(p2)) => p1 == p2,
(
BitField(size1, Some((offset1, type1)), level1),
Expand Down
15 changes: 14 additions & 1 deletion crates/objc2-encode/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;

use crate::helper::{ContainerKind, Helper, NestingLevel};
use crate::helper::{ContainerKind, Helper, NestingLevel, Primitive};
use crate::{Encoding, EncodingBox};

/// Check whether a struct or union name is a valid identifier
Expand Down Expand Up @@ -251,7 +251,20 @@ impl Parser<'_> {

pub(crate) fn expect_encoding(&mut self, enc: &Encoding, level: NestingLevel) -> Option<()> {
let helper = Helper::new(enc, level);
if level.should_void_allow_non_void() && self.expect_byte(b'v').is_some() {
return Some(());
}
match helper {
Helper::Primitive(Primitive::Void) if level.should_void_allow_non_void() => {
if self.expect_byte(b'v').is_some() {
Some(())
} else {
match self.parse_encoding() {
Ok(_) => Some(()),
Err(_) => None,
}
}
}
Helper::Primitive(primitive) => self.expect_str(primitive.to_str()),
Helper::BitField(size, Some((offset, t)), level) => {
self.expect_byte(b'b')?;
Expand Down

0 comments on commit 2f990df

Please sign in to comment.