Rewrite From/TryFrom conversions on generated types to avoid future orphan rule violations (#34554)

* script: Add traits to allow converting between types that are not defined in the script crate.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Rewrite all From/TryFrom implementations on generated WebIDL types to use new Convert/TryConvert traits.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2024-12-11 00:52:48 -05:00 committed by GitHub
parent e10e989abb
commit e0cbab2fbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 439 additions and 380 deletions

View file

@ -9,6 +9,7 @@ use dom_struct::dom_struct;
use js::rust::HandleObject;
use servo_media::audio::context::{LatencyCategory, ProcessingState, RealTimeAudioContextOptions};
use crate::conversions::Convert;
use crate::dom::baseaudiocontext::{BaseAudioContext, BaseAudioContextOptions};
use crate::dom::bindings::codegen::Bindings::AudioContextBinding::{
AudioContextLatencyCategory, AudioContextMethods, AudioContextOptions, AudioTimestamp,
@ -55,7 +56,7 @@ impl AudioContext {
) -> Fallible<AudioContext> {
// Steps 1-3.
let context = BaseAudioContext::new_inherited(
BaseAudioContextOptions::AudioContext(options.into()),
BaseAudioContextOptions::AudioContext(options.convert()),
pipeline_id,
)?;
@ -305,9 +306,9 @@ impl AudioContextMethods<crate::DomTypeHolder> for AudioContext {
}
}
impl From<AudioContextLatencyCategory> for LatencyCategory {
fn from(category: AudioContextLatencyCategory) -> Self {
match category {
impl Convert<LatencyCategory> for AudioContextLatencyCategory {
fn convert(self) -> LatencyCategory {
match self {
AudioContextLatencyCategory::Balanced => LatencyCategory::Balanced,
AudioContextLatencyCategory::Interactive => LatencyCategory::Interactive,
AudioContextLatencyCategory::Playback => LatencyCategory::Playback,
@ -315,13 +316,13 @@ impl From<AudioContextLatencyCategory> for LatencyCategory {
}
}
impl<'a> From<&'a AudioContextOptions> for RealTimeAudioContextOptions {
fn from(options: &AudioContextOptions) -> Self {
Self {
sample_rate: *options.sampleRate.unwrap_or(Finite::wrap(44100.)),
latency_hint: match options.latencyHint {
impl<'a> Convert<RealTimeAudioContextOptions> for &'a AudioContextOptions {
fn convert(self) -> RealTimeAudioContextOptions {
RealTimeAudioContextOptions {
sample_rate: *self.sampleRate.unwrap_or(Finite::wrap(44100.)),
latency_hint: match self.latencyHint {
AudioContextLatencyCategoryOrDouble::AudioContextLatencyCategory(category) => {
category.into()
category.convert()
},
AudioContextLatencyCategoryOrDouble::Double(_) => LatencyCategory::Interactive, // TODO
},