Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions crates/rspack_core/src/utils/deref_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ use std::{
ops::{Deref, DerefMut},
};

#[derive(Debug, Default)]
#[derive(Debug)]
pub struct DerefOption<T>(Option<T>);

impl<T> Default for DerefOption<T>
where
T: Default,
{
Comment on lines +9 to +12
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

Consider adding documentation to clarify the behavior of the Default implementation. For example:

/// Creates a `DerefOption<T>` with `Some(T::default())`.
/// This preserves the default behavior when wrapping types that implement `Default`.
impl<T> Default for DerefOption<T>

This helps users understand that the default is Some(T::default()) rather than None, which is non-obvious for an Option wrapper.

Copilot uses AI. Check for mistakes.
fn default() -> Self {
Self(Some(T::default()))
}
}
Comment on lines +9 to +16
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The new Default implementation that returns Some(T::default()) should have test coverage to ensure it behaves as expected. Consider adding a test module similar to other utility files in this directory (e.g., fs_trim.rs, incremental_info.rs). Example test:

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_default_creates_some() {
    #[derive(Default, Debug, PartialEq)]
    struct TestStruct {
      value: i32,
    }

    let deref_option: DerefOption<TestStruct> = Default::default();
    assert_eq!(*deref_option, TestStruct::default());
  }
}

Copilot uses AI. Check for mistakes.

impl<T> From<T> for DerefOption<T> {
fn from(value: T) -> Self {
Self::new(value)
Expand Down Expand Up @@ -40,10 +49,7 @@ impl<T> Deref for DerefOption<T> {
.unwrap_or_else(|| panic!("should set in compilation first"))
}
}
impl<T> DerefMut for DerefOption<T>
where
T: Debug,
{
impl<T> DerefMut for DerefOption<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self
.0
Expand Down
Loading