Use wl_container_of() instead of casts

This slightly improves type safety.

The culprits were found with:

    git grep -E '\([a-z0-9_ ]+ \*\)\W?[a-z]'
This commit is contained in:
Simon Ser 2023-07-11 17:54:08 +02:00
parent c2c536de03
commit fe06e5f49a
29 changed files with 85 additions and 55 deletions

View file

@ -7,11 +7,12 @@
static const struct wlr_buffer_impl client_buffer_impl;
struct wlr_client_buffer *wlr_client_buffer_get(struct wlr_buffer *buffer) {
if (buffer->impl != &client_buffer_impl) {
struct wlr_client_buffer *wlr_client_buffer_get(struct wlr_buffer *wlr_buffer) {
if (wlr_buffer->impl != &client_buffer_impl) {
return NULL;
}
return (struct wlr_client_buffer *)buffer;
struct wlr_client_buffer *buffer = wl_container_of(wlr_buffer, buffer, base);
return buffer;
}
static struct wlr_client_buffer *client_buffer_from_buffer(

View file

@ -7,9 +7,10 @@
static const struct wlr_buffer_impl dmabuf_buffer_impl;
static struct wlr_dmabuf_buffer *dmabuf_buffer_from_buffer(
struct wlr_buffer *buffer) {
assert(buffer->impl == &dmabuf_buffer_impl);
return (struct wlr_dmabuf_buffer *)buffer;
struct wlr_buffer *wlr_buffer) {
assert(wlr_buffer->impl == &dmabuf_buffer_impl);
struct wlr_dmabuf_buffer *buffer = wl_container_of(wlr_buffer, buffer, base);
return buffer;
}
static void dmabuf_buffer_destroy(struct wlr_buffer *wlr_buffer) {

View file

@ -8,9 +8,10 @@
static const struct wlr_buffer_impl readonly_data_buffer_impl;
static struct wlr_readonly_data_buffer *readonly_data_buffer_from_buffer(
struct wlr_buffer *buffer) {
assert(buffer->impl == &readonly_data_buffer_impl);
return (struct wlr_readonly_data_buffer *)buffer;
struct wlr_buffer *wlr_buffer) {
assert(wlr_buffer->impl == &readonly_data_buffer_impl);
struct wlr_readonly_data_buffer *buffer = wl_container_of(wlr_buffer, buffer, base);
return buffer;
}
static void readonly_data_buffer_destroy(struct wlr_buffer *wlr_buffer) {

View file

@ -91,7 +91,8 @@ static void client_data_source_accept(struct wlr_data_source *wlr_source,
static struct wlr_client_data_source *client_data_source_from_wlr_data_source(
struct wlr_data_source *wlr_source) {
assert(wlr_source->impl->accept == client_data_source_accept);
return (struct wlr_client_data_source *)wlr_source;
struct wlr_client_data_source *source = wl_container_of(wlr_source, source, source);
return source;
}
static void client_data_source_accept(struct wlr_data_source *wlr_source,

View file

@ -52,7 +52,8 @@ struct wlr_scene *scene_node_get_root(struct wlr_scene_node *node) {
while (tree->node.parent != NULL) {
tree = tree->node.parent;
}
return (struct wlr_scene *)tree;
struct wlr_scene *scene = wl_container_of(tree, scene, tree);
return scene;
}
static void scene_node_init(struct wlr_scene_node *node,

View file

@ -120,7 +120,8 @@ static const struct wlr_data_source_impl client_source_impl;
static struct client_data_source *
client_data_source_from_source(struct wlr_data_source *wlr_source) {
assert(wlr_source->impl == &client_source_impl);
return (struct client_data_source *)wlr_source;
struct client_data_source *source = wl_container_of(wlr_source, source, source);
return source;
}
static void client_source_send(struct wlr_data_source *wlr_source,
@ -166,7 +167,8 @@ static struct client_primary_selection_source *
client_primary_selection_source_from_source(
struct wlr_primary_selection_source *wlr_source) {
assert(wlr_source->impl == &client_primary_selection_source_impl);
return (struct client_primary_selection_source *)wlr_source;
struct client_primary_selection_source *source = wl_container_of(wlr_source, source, source);
return source;
}
static void client_primary_selection_source_send(

View file

@ -27,9 +27,10 @@ static const struct wl_buffer_interface wl_buffer_impl = {
static const struct wlr_buffer_impl buffer_impl;
static struct wlr_drm_buffer *drm_buffer_from_buffer(
struct wlr_buffer *buffer) {
assert(buffer->impl == &buffer_impl);
return (struct wlr_drm_buffer *)buffer;
struct wlr_buffer *wlr_buffer) {
assert(wlr_buffer->impl == &buffer_impl);
struct wlr_drm_buffer *buffer = wl_container_of(wlr_buffer, buffer, base);
return buffer;
}
static void buffer_destroy(struct wlr_buffer *wlr_buffer) {

View file

@ -64,7 +64,8 @@ struct wlr_keyboard_group *wlr_keyboard_group_from_wlr_keyboard(
if (keyboard->impl != &impl) {
return NULL;
}
return (struct wlr_keyboard_group *)keyboard;
struct wlr_keyboard_group *group = wl_container_of(keyboard, group, keyboard);
return group;
}
static bool process_key(struct keyboard_group_device *group_device,

View file

@ -90,9 +90,10 @@ struct wlr_dmabuf_v1_buffer *wlr_dmabuf_v1_buffer_from_buffer_resource(
static const struct wlr_buffer_impl buffer_impl;
static struct wlr_dmabuf_v1_buffer *dmabuf_v1_buffer_from_buffer(
struct wlr_buffer *buffer) {
assert(buffer->impl == &buffer_impl);
return (struct wlr_dmabuf_v1_buffer *)buffer;
struct wlr_buffer *wlr_buffer) {
assert(wlr_buffer->impl == &buffer_impl);
struct wlr_dmabuf_v1_buffer *buffer = wl_container_of(wlr_buffer, buffer, base);
return buffer;
}
static void buffer_destroy(struct wlr_buffer *wlr_buffer) {

View file

@ -103,14 +103,14 @@ struct client_data_source {
static void client_source_send(
struct wlr_primary_selection_source *wlr_source,
const char *mime_type, int fd) {
struct client_data_source *source = (struct client_data_source *)wlr_source;
struct client_data_source *source = wl_container_of(wlr_source, source, source);
zwp_primary_selection_source_v1_send_send(source->resource, mime_type, fd);
close(fd);
}
static void client_source_destroy(
struct wlr_primary_selection_source *wlr_source) {
struct client_data_source *source = (struct client_data_source *)wlr_source;
struct client_data_source *source = wl_container_of(wlr_source, source, source);
zwp_primary_selection_source_v1_send_cancelled(source->resource);
// Make the source resource inert
wl_resource_set_user_data(source->resource, NULL);