Skip to content
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

Implement ZeroMapKV for Pattern<SinglePlaceholder, str> #5030

Merged
merged 33 commits into from
Jun 21, 2024

Conversation

younies
Copy link
Member

@younies younies commented Jun 10, 2024

No description provided.

@younies younies requested review from sffc and Manishearth June 10, 2024 21:13
@younies younies requested a review from zbraniecki as a code owner June 10, 2024 21:13

use crate::{Pattern, SinglePlaceholder};

impl<'data> ZeroMapKV<'data> for Pattern<SinglePlaceholder, Cow<'data, str>> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sffc and @Manishearth

I would like if you can give me your help in this implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it should be something like this ?

impl<'data> ZeroMapKV<'data> for Pattern<SinglePlaceholder, Cow<'data, str>> {
    type Container = ZeroVec<Pattern<SinglePlaceholder, Cow<'data, str>>>;
    type Slice = ZeroVec<Pattern<SinglePlaceholder, Cow<'data, str>>>;
    type GetType = Cow<'data, str>;
    type OwnedType = Cow<'data, str>;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be more like

impl<'data> ZeroMapKV<'data> for Pattern<SinglePlaceholder, str> {
    type Container = VarZeroVec<'data, Pattern<SinglePlaceholder, str>>;
    type Slice = VarZeroSlice<Pattern<SinglePlaceholder, str>>;
    type GetType = Pattern<SinglePlaceholder, str>;
    type OwnedType = Pattern<SinglePlaceholder, String>;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still some errors, I will try to fix them in the morning.

@younies younies removed the request for review from zbraniecki June 10, 2024 21:22
@sffc sffc marked this pull request as draft June 11, 2024 07:45
@younies younies marked this pull request as ready for review June 11, 2024 11:23
@younies younies requested a review from eggrobin June 11, 2024 11:23
@younies younies removed the request for review from eggrobin June 11, 2024 11:50
@@ -55,6 +55,7 @@ mod common;
mod double;
mod error;
mod frontend;
mod implementations;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: make this

#[cfg(feature = "zerovec")]
mod zerovec;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -29,10 +29,10 @@ litemap = { workspace = true, default-features = false, optional = true }
serde = { workspace = true, features = ["derive", "alloc"], optional = true }
yoke = { workspace = true, features = ["derive"], optional = true }
zerofrom = { workspace = true, features = ["derive"], optional = true }
zerovec = { workspace = true, features = ["databake", "serde", "yoke"], optional = true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add an explicit zerovec = ["dep:zerovec"] feature


use crate::{Pattern, SinglePlaceholder};

impl<'data> ZeroMapKV<'data> for Pattern<SinglePlaceholder, str> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add the impl for all pattern backends defined in this crate

@sffc sffc marked this pull request as draft June 11, 2024 11:54
@sffc
Copy link
Member

sffc commented Jun 11, 2024

This PR is a draft because it doesn't build.

}

unsafe impl VarULE for Pattern<SinglePlaceholder, str> {
fn validate_byte_slice(_bytes: &[u8]) -> Result<(), zerovec::ZeroVecError> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sffc , is that the correct direction, or I can implement it in a different way ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you'll need this impl. You should add a try_from_utf8 function on Pattern and call it from here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean try_from_u8 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean try_from_utf8 as agreed in #4931

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

utils/pattern/src/implementations.rs Outdated Show resolved Hide resolved
Comment on lines 25 to 26
let store = core::str::from_utf8_unchecked(bytes);
SinglePlaceholderPattern::from_borrowed_store_unchecked(store)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: justify these unsafe function calls

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong way around, you need to explain why each precondition of from_utf8_unchecked and of from_borrowed_store_unchecked holds.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, but I do not think I need to explain what each function do, the user can check this if they want.

type Container = VarZeroVec<'a, Pattern<SinglePlaceholder, str>>;
type Slice = VarZeroSlice<Pattern<SinglePlaceholder, str>>;
type GetType = Pattern<SinglePlaceholder, str>;
type OwnedType = Box<Pattern<SinglePlaceholder, str>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be Pattern<SinglePlaceholder, String>?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes if it's easy, but not super important

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just another transmute

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it should be Pattern<SinglePlaceholder, String> , if we are using Pattern<SinglePlaceholder, str> in the datagen ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the implementation will be : #5084

@younies younies requested a review from robertbastian June 19, 2024 14:35
}

#[cfg(feature = "alloc")]
unsafe impl VarULE for Pattern<SinglePlaceholder, str> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought: this impl is narrower than required; better would be an impl where Store: VarULE

Non blocking because we can generalize later. But up to this point we've done a good job of keeping the impls general.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I am thinking also to add imple to Double Pattern or generalize this later.

@younies younies requested a review from sffc June 19, 2024 18:52
.map_err(|_| ZeroVecError::VarZeroVecFormatError)?;
Ok(())
}
/// SAFETY: The `bytes` slice must be validated by `Self::validate_byte_slice`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the trait defines the safety invariants for this method, so you don't have to repeat it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

utils/pattern/src/implementations.rs Outdated Show resolved Hide resolved
utils/pattern/src/implementations.rs Outdated Show resolved Hide resolved
@younies younies requested a review from robertbastian June 20, 2024 12:58
type OwnedType = Box<Pattern<SinglePlaceholder, str>>;
}

unsafe impl VarULE for Pattern<SinglePlaceholder, str> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The impl itself is unsafe, which means there are invariants on the trait that need to be followed: https://docs.rs/zerovec/latest/zerovec/ule/trait.VarULE.html#safety. Please check these and leave a comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@younies younies requested a review from robertbastian June 21, 2024 14:03
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this is good and unblocks other work so we should merge it. I was hoping to see a general VarULE impl here, but it's harder to get the trait bounds and safety invariants right, so we don't need to block on it.

@younies younies merged commit ba0bcd1 into unicode-org:main Jun 21, 2024
28 checks passed
@younies younies deleted the dev-develop-kv branch June 21, 2024 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants