Repository: servo/servo
File: components/shared/base/generic_channel/shared_memory.rs (Rust)

Code before:
    fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::EnumAccess<'de>,
    {
        #[derive(Deserialize)]
        enum GenericSharedMemoryVariantNames {
            Ipc,
            InProcess,
        }

        let (variant_name, variant_data): (GenericSharedMemoryVariantNames, _) = data.variant()?;

        match variant_name {
            GenericSharedMemoryVariantNames::Ipc => variant_data
                .newtype_variant::<IpcSharedMemory>()
                .map(|receiver| GenericSharedMemory(GenericSharedMemoryVariant::Ipc(receiver))),
            GenericSharedMemoryVariantNames::InProcess => {
                if opts::get().multiprocess {
                    return Err(serde::de::Error::custom(
                        "Crossbeam channel found in multiprocess mode!",
                    ));
                }
                let addr = variant_data.newtype_variant::<usize>()?;
                let ptr = addr as *mut Vec<u8>;
                // SAFETY: We know we are in the same address space as the sender, so we can safely
                // reconstruct the Box.
                #[expect(unsafe_code)]
                let arc = unsafe { Arc::from_raw(ptr) };
                Ok(GenericSharedMemory(GenericSharedMemoryVariant::InProcess(
                    arc,
                )))
            },
        }
    }
}

impl<'a> Deserialize<'a> for GenericSharedMemory {
    fn deserialize<D>(d: D) -> Result<GenericSharedMemory, D::Error>
    where
        D: Deserializer<'a>,
    {
        d.deserialize_enum(
            "GenericSharedMemory",
            &["Ipc", "InProcess"],
            GenericSharedMemoryVisitor {},
        )
    }
}

#[cfg(test)]
mod single_process_callback_test {

Review comment (suggestion):
```suggestion
                // reconstruct the Arc.
```

Code after:
    fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::EnumAccess<'de>,
    {
        #[derive(Deserialize)]
        enum GenericSharedMemoryVariantNames {
            Ipc,
            InProcess,
        }

        let (variant_name, variant_data): (GenericSharedMemoryVariantNames, _) = data.variant()?;

        match variant_name {
            GenericSharedMemoryVariantNames::Ipc => variant_data
                .newtype_variant::<IpcSharedMemory>()
                .map(|receiver| GenericSharedMemory(GenericSharedMemoryVariant::Ipc(receiver))),
            GenericSharedMemoryVariantNames::InProcess => {
                if opts::get().multiprocess || servo_config::opts::get().force_ipc {
                    return Err(serde::de::Error::custom(
                        "Arc data found in multiprocess mode!",
                    ));
                }
                let addr = variant_data.newtype_variant::<usize>()?;
                let ptr = addr as *mut Vec<u8>;
                // SAFETY: We know we are in the same address space as the sender, so we can safely
                // reconstruct the Arc.
                #[expect(unsafe_code)]
                let arc = unsafe { Arc::from_raw(ptr) };
                Ok(GenericSharedMemory(GenericSharedMemoryVariant::InProcess(
                    arc,
                )))
            },
        }
    }
}

impl<'a> Deserialize<'a> for GenericSharedMemory {
    fn deserialize<D>(d: D) -> Result<GenericSharedMemory, D::Error>
    where
        D: Deserializer<'a>,
    {
        d.deserialize_enum(
            "GenericSharedMemory",
            &["Ipc", "InProcess"],
            GenericSharedMemoryVisitor {},
        )
    }
}

#[cfg(test)]
mod single_process_shared_memory_test {