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

Alow manipulating Selector parts from consumer code #561

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,47 @@ impl<'i, Impl: SelectorImpl<'i>> Selector<'i, Impl> {
self.1.insert(index, component);
}

/// Inserts a [`Component`] to an arbitrary index in the internal components Vec.
///
/// An order in which components are stored is right-to-left (see [`Selector::iter_raw_match_order()`])
#[inline]
pub fn insert_raw(&mut self, index: usize, component: Component<'i, Impl>) {
self.1.insert(index, component)
}

/// Inserts multiple [`Component`]s to an arbitrary index in the internal components Vec.
///
/// An order in which components are stored is right-to-left (see [`Selector::iter_raw_match_order()`])
#[inline]
pub fn insert_raw_multiple(&mut self, index: usize, mut components: Vec<Component<'i, Impl>>) {
if index > self.1.len() {
return;
}

let old_len = self.1.len();
let mut old = std::mem::replace(&mut self.1, Vec::with_capacity(old_len + components.len()));

// Fast track: insert at index 0 is the same as Vec::append
if index == 0 {
self.1.append(&mut components);
self.1.append(&mut old);
return;
}

// Manually move the first `index` elements
let mut old_iter = old.into_iter();
for _ in 0..index {
let Some(old_component) = old_iter.next() else {
unreachable!("index is never greater than selector len")
};

self.1.push(old_component)
}

self.1.append(&mut components);
self.1.extend(old_iter);
}

#[inline]
pub fn parts(&self) -> Option<&[Impl::Identifier]> {
if !self.is_part() {
Expand Down