Add add_metadata_checked method (#3008)

* new api

* found a bug :>
This commit is contained in:
Dongjia "toka" Zhang 2025-02-19 20:24:15 +01:00 committed by GitHub
parent 9f28672ea1
commit 98ef505a0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 35 additions and 8 deletions

View File

@ -26,6 +26,23 @@ pub trait HasMetadata {
self.metadata_map_mut().insert(meta); self.metadata_map_mut().insert(meta);
} }
/// Add a metadata to the metadata map
/// Returns error if the metadata is already there
#[inline]
fn add_metadata_checked<M>(&mut self, meta: M) -> Result<(), Error>
where
M: SerdeAny,
{
if self.has_metadata::<M>() {
return Err(Error::illegal_argument(format!(
"Tried to add a metadata of {}. But this will overwrite the existing metadata",
type_name::<M>()
)));
}
self.metadata_map_mut().insert(meta);
Ok(())
}
/// Gets metadata, or inserts it using the given construction function `default` /// Gets metadata, or inserts it using the given construction function `default`
fn metadata_or_insert_with<M>(&mut self, default: impl FnOnce() -> M) -> &mut M fn metadata_or_insert_with<M>(&mut self, default: impl FnOnce() -> M) -> &mut M
where where
@ -94,6 +111,20 @@ pub trait HasNamedMetadata {
self.named_metadata_map_mut().insert(name, meta); self.named_metadata_map_mut().insert(name, meta);
} }
/// Add a metadata to the metadata map
/// Return an error if there already is the metadata with the same name
#[inline]
fn add_named_metadata_checked<M>(&mut self, name: &str, meta: M) -> Result<(), Error>
where
M: SerdeAny,
{
if self.has_named_metadata::<M>(name) {
return Err(Error::illegal_argument(format!("Tried to add a metadata of {} named {}. But this will overwrite the existing metadata", type_name::<M>(), name)));
}
self.named_metadata_map_mut().insert(name, meta);
Ok(())
}
/// Add a metadata to the metadata map /// Add a metadata to the metadata map
#[inline] #[inline]
fn remove_named_metadata<M>(&mut self, name: &str) -> Option<Box<M>> fn remove_named_metadata<M>(&mut self, name: &str) -> Option<Box<M>>

View File

@ -118,7 +118,7 @@ where
T: Debug + Eq + Hash + for<'a> Deserialize<'a> + Serialize + Default + Copy + 'static, T: Debug + Eq + Hash + for<'a> Deserialize<'a> + Serialize + Default + Copy + 'static,
{ {
fn init_state(&mut self, state: &mut S) -> Result<(), Error> { fn init_state(&mut self, state: &mut S) -> Result<(), Error> {
state.add_named_metadata(self.name(), ListFeedbackMetadata::<T>::default()); state.add_named_metadata_checked(self.name(), ListFeedbackMetadata::<T>::default())?;
Ok(()) Ok(())
} }
} }

View File

@ -386,7 +386,7 @@ where
fn init_state(&mut self, state: &mut S) -> Result<(), Error> { fn init_state(&mut self, state: &mut S) -> Result<(), Error> {
// Initialize `MapFeedbackMetadata` with an empty vector and add it to the state. // Initialize `MapFeedbackMetadata` with an empty vector and add it to the state.
// The `MapFeedbackMetadata` would be resized on-demand in `is_interesting` // The `MapFeedbackMetadata` would be resized on-demand in `is_interesting`
state.add_named_metadata(&self.name, MapFeedbackMetadata::<O::Entry>::default()); state.add_named_metadata_checked(&self.name, MapFeedbackMetadata::<O::Entry>::default())?;
Ok(()) Ok(())
} }
} }

View File

@ -141,10 +141,10 @@ where
S: HasNamedMetadata, S: HasNamedMetadata,
{ {
fn init_state(&mut self, state: &mut S) -> Result<(), Error> { fn init_state(&mut self, state: &mut S) -> Result<(), Error> {
state.add_named_metadata( state.add_named_metadata_checked(
&self.name, &self.name,
NewHashFeedbackMetadata::with_capacity(self.capacity), NewHashFeedbackMetadata::with_capacity(self.capacity),
); )?;
Ok(()) Ok(())
} }
} }

View File

@ -68,7 +68,6 @@ pub use owned_map::*;
/// // inform the feedback to track indices (required by IndexesLenTimeMinimizerScheduler), but not novelties /// // inform the feedback to track indices (required by IndexesLenTimeMinimizerScheduler), but not novelties
/// // this *MUST* be done before it is passed to MaxMapFeedback! /// // this *MUST* be done before it is passed to MaxMapFeedback!
/// let edges_observer = edges_observer.track_indices(); /// let edges_observer = edges_observer.track_indices();
///
/// // init the feedback /// // init the feedback
/// let mut feedback = MaxMapFeedback::new(&edges_observer); /// let mut feedback = MaxMapFeedback::new(&edges_observer);
/// # /// #
@ -80,9 +79,6 @@ pub use owned_map::*;
/// # &mut feedback, /// # &mut feedback,
/// # &mut (), /// # &mut (),
/// # ).unwrap(); /// # ).unwrap();
///
/// # feedback.init_state(&mut state).unwrap();
///
/// let scheduler = IndexesLenTimeMinimizerScheduler::new(&edges_observer, QueueScheduler::new()); /// let scheduler = IndexesLenTimeMinimizerScheduler::new(&edges_observer, QueueScheduler::new());
/// # scheduler.cull(&state).unwrap(); /// # scheduler.cull(&state).unwrap();
/// ``` /// ```