Skip to content

Convert Context into an enum - #399

Merged
sagudev merged 2 commits into
servo:mainfrom
mrobinson:context-enum
Sep 28, 2026
Merged

sagudev merged 2 commits into
servo:mainfrom
mrobinson:context-enum

Conversation

@mrobinson

@mrobinson mrobinson commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

This requires a bit more error handling than before, but some of this
will be cleaned up once all data structures are converted to enums.

This requires a bit more error handling than before, but some of this
will be cleaned up once all data structures are converted to enums.

Signed-off-by: Martin Robinson <martin@abandonedwig.info>
@mrobinson mrobinson mentioned this pull request Sep 27, 2026
3 of 6 tasks

@sagudev sagudev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

comment(non-blocking): One thing that wgpu also has are traits: https://docs.rs/wgpu-hal/latest/wgpu_hal/trait.Api.html. Every type implements a trait (with it's types provided via associated types) and even dispatch type implements it (associated types are also dispatch types). I think that would allow us to simplify and unify more.

Comment thread src/angle/device.rs Outdated
descriptor: &ContextDescriptor,
share_with: Option<&Context>,
) -> Result<Context, Error> {
let share_with = match share_with {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit(non-blocking):

let share_with = share_with.map(|x| x.angle()).transpose()?;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, cool! I've done this everywhere.

Comment thread src/angle/device.rs Outdated
Comment on lines +354 to +358
let context: &mut AngleContext = context.try_into()?;
if context.egl_context == egl::NO_CONTEXT {
return Ok(());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why moving the check? This can have bad implications.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was to avoid unpacking the AngleContext more than once (unbind_surface_from_context and destroy_surface both take a Context). I've moved this back to the origin order and now just unpack it more than once, the first time non-mutably.

Comment on lines +200 to +203
let context: &mut HardwareBufferContext = match context.try_into() {
Ok(context) => context,
Err(error) => return Err((error, surface)),
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit(non-blocking):

Suggested change
let context: &mut HardwareBufferContext = match context.try_into() {
Ok(context) => context,
Err(error) => return Err((error, surface)),
};
let context: &mut HardwareBufferContext = context.try_into().map_err(|error| (error, surface))?;

(it might also need as_mut)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did try this initially, but it doesn't work, because surface moves into the closure and the method itself is taking ownership and needs to use it later in the non-error path.

Signed-off-by: Martin Robinson <martin@abandonedwig.info>

@mrobinson mrobinson left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the review. I think I've addressed the comments that I could.

Comment thread src/angle/device.rs Outdated
descriptor: &ContextDescriptor,
share_with: Option<&Context>,
) -> Result<Context, Error> {
let share_with = match share_with {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, cool! I've done this everywhere.

Comment thread src/angle/device.rs Outdated
Comment on lines +354 to +358
let context: &mut AngleContext = context.try_into()?;
if context.egl_context == egl::NO_CONTEXT {
return Ok(());
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was to avoid unpacking the AngleContext more than once (unbind_surface_from_context and destroy_surface both take a Context). I've moved this back to the origin order and now just unpack it more than once, the first time non-mutably.

Comment on lines +200 to +203
let context: &mut HardwareBufferContext = match context.try_into() {
Ok(context) => context,
Err(error) => return Err((error, surface)),
};

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did try this initially, but it doesn't work, because surface moves into the closure and the method itself is taking ownership and needs to use it later in the non-error path.

@sagudev
sagudev added this pull request to the merge queue Sep 28, 2026
Merged via the queue into servo:main with commit 3bdaf39 Sep 28, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants