Skip to content

Commit

Permalink
WIP destroy_object
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed May 13, 2024
1 parent 5e5ffc2 commit f8975f7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
14 changes: 13 additions & 1 deletion wayland-backend/src/rs/server_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ impl<D> Client<D> {
InnerObjectId { id, serial, client_id: self.id.clone(), interface }
}

pub(crate) fn destroy_object(
&mut self,
id: InnerObjectId,
pending_destructors: &mut Vec<super::handle::PendingDestructor<D>>,
) -> Result<(), InvalidId> {
let object = self.get_object(id.clone())?;
self.map.remove(id.id);
pending_destructors.push((object.data.user_data.clone(), self.id.clone(), id.clone()));
self.send_delete_id(id.clone());
Ok(())
}

pub(crate) fn object_info(&self, id: InnerObjectId) -> Result<ObjectInfo, InvalidId> {
let object = self.get_object(id.clone())?;
Ok(ObjectInfo { id: id.id, interface: object.interface, version: object.version })
Expand Down Expand Up @@ -376,7 +388,7 @@ impl<D> Client<D> {
}
}

fn get_object(&self, id: InnerObjectId) -> Result<Object<Data<D>>, InvalidId> {
pub(crate) fn get_object(&self, id: InnerObjectId) -> Result<Object<Data<D>>, InvalidId> {
let object = self.map.find(id.id).ok_or(InvalidId)?;
if object.data.serial != id.serial {
return Err(InvalidId);
Expand Down
9 changes: 9 additions & 0 deletions wayland-backend/src/rs/server_impl/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ impl InnerHandle {
Ok(ObjectId { id: client.create_object(interface, version, data) })
}

pub fn destroy_object<D: 'static>(&self, id: &ObjectId) -> Result<(), InvalidId> {
let mut state = self.state.lock().unwrap();
let state = (&mut *state as &mut dyn ErasedState)
.downcast_mut::<State<D>>()
.expect("Wrong type parameter passed to Handle::destroy_object().");
let client = state.clients.get_client_mut(id.id.client_id.clone())?;
client.destroy_object(id.id.clone(), &mut state.pending_destructors)
}

pub fn null_id() -> ObjectId {
ObjectId {
id: InnerObjectId {
Expand Down
4 changes: 4 additions & 0 deletions wayland-backend/src/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ impl Handle {
self.handle.create_object(client_id.id, interface, version, data)
}

pub fn destroy_object<D: 'static>(&self, id: &ObjectId) {

Check failure on line 358 in wayland-backend/src/server_api.rs

View workflow job for this annotation

GitHub Actions / clippy-check

missing documentation for a method

Check failure on line 358 in wayland-backend/src/server_api.rs

View workflow job for this annotation

GitHub Actions / clippy-check

missing documentation for a method
self.handle.destroy_object::<D>(id);

Check failure on line 359 in wayland-backend/src/server_api.rs

View workflow job for this annotation

GitHub Actions / clippy-check

unused `std::result::Result` that must be used
}

/// Send an event to the client
///
/// Returns an error if the sender ID of the provided message is no longer valid.
Expand Down
12 changes: 12 additions & 0 deletions wayland-backend/src/sys/server_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,18 @@ impl InnerHandle {
Ok(ObjectId { id: unsafe { init_resource(resource, interface, Some(data)).0 } })
}

pub fn destroy_object<D: 'static>(&self, id: &ObjectId) {
let mut state = self.state.lock().unwrap();
// Keep this guard alive while the code is run to protect the C state
let state = (&mut *state as &mut dyn ErasedState)
.downcast_mut::<State<D>>()
.expect("Wrong type parameter passed to Handle::destroy_object().");

PENDING_DESTRUCTORS.set(&(&mut state.pending_destructors as *mut _ as *mut _), || unsafe {
ffi_dispatch!(wayland_server_handle(), wl_resource_destroy, id.id.ptr);
});
}

pub fn null_id() -> ObjectId {
ObjectId {
id: InnerObjectId {
Expand Down

0 comments on commit f8975f7

Please sign in to comment.