Skip to content

Commit

Permalink
commons: fix server ids handling in map
Browse files Browse the repository at this point in the history
Client ids start at 1 and server ids start at SERVER_ID_LIMIT, not
SERVER_ID_LIMIT+1.

cc #224
  • Loading branch information
elinorbgr authored and vberger committed Jan 18, 2019
1 parent d192197 commit 738fc3b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- [commons] Fix incorrect number of server-side ids when using the rust implementation

## 0.21.10 -- 2019-01-06

- [client] Undo protocol update which was a breakind change
Expand Down
4 changes: 4 additions & 0 deletions tests/server_created_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ fn data_offer() {
.create_resource::<ServerDO>(ddevice.version())
.unwrap()
.implement(|_, _| {}, None::<fn(_)>, ());
// this must be the first server-side ID
assert_eq!(offer.id(), 0xFF000000);
ddevice.send(SDDEvt::DataOffer { id: offer })
}
_ => unimplemented!(),
Expand Down Expand Up @@ -63,6 +65,8 @@ fn data_offer() {
CDDEvt::DataOffer { id } => {
let doffer = id.implement(|_, _| {}, ());
assert!(doffer.version() == 3);
// this must be the first server-side ID
assert_eq!(doffer.id(), 0xFF000000);
*received2.lock().unwrap() = true;
}
_ => unimplemented!(),
Expand Down
18 changes: 8 additions & 10 deletions wayland-commons/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<Meta: ObjectMetadata> ObjectMap<Meta> {
pub fn find(&self, id: u32) -> Option<Object<Meta>> {
if id >= SERVER_ID_LIMIT {
self.server_objects
.get((id - SERVER_ID_LIMIT - 1) as usize)
.get((id - SERVER_ID_LIMIT) as usize)
.and_then(|x| x.clone())
} else {
self.client_objects.get((id - 1) as usize).and_then(|x| x.clone())
Expand All @@ -129,7 +129,7 @@ impl<Meta: ObjectMetadata> ObjectMap<Meta> {
/// Does nothing if the object didn't previously exists
pub fn remove(&mut self, id: u32) {
if id >= SERVER_ID_LIMIT {
if let Some(place) = self.server_objects.get_mut((id - SERVER_ID_LIMIT - 1) as usize) {
if let Some(place) = self.server_objects.get_mut((id - SERVER_ID_LIMIT) as usize) {
*place = None;
}
} else {
Expand All @@ -147,13 +147,13 @@ impl<Meta: ObjectMetadata> ObjectMap<Meta> {
if id >= SERVER_ID_LIMIT {
insert_in_at(&mut self.server_objects, (id - SERVER_ID_LIMIT) as usize, object)
} else {
insert_in_at(&mut self.client_objects, id as usize, object)
insert_in_at(&mut self.client_objects, (id - 1) as usize, object)
}
}

/// Allocate a new id for an object in the client namespace
pub fn client_insert_new(&mut self, object: Object<Meta>) -> u32 {
insert_in(&mut self.client_objects, object)
insert_in(&mut self.client_objects, object) + 1
}

/// Allocate a new id for an object in the server namespace
Expand All @@ -164,8 +164,7 @@ impl<Meta: ObjectMetadata> ObjectMap<Meta> {
/// Mutably access an object of the map
pub fn with<T, F: FnOnce(&mut Object<Meta>) -> T>(&mut self, id: u32, f: F) -> Result<T, ()> {
if id >= SERVER_ID_LIMIT {
if let Some(&mut Some(ref mut obj)) =
self.server_objects.get_mut((id - SERVER_ID_LIMIT - 1) as usize)
if let Some(&mut Some(ref mut obj)) = self.server_objects.get_mut((id - SERVER_ID_LIMIT) as usize)
{
Ok(f(obj))
} else {
Expand All @@ -189,7 +188,7 @@ impl<Meta: ObjectMetadata> ObjectMap<Meta> {
}
for (id, place) in self.server_objects.iter_mut().enumerate() {
if let Some(ref mut obj) = *place {
f(id as u32 + 1 + SERVER_ID_LIMIT, obj);
f(id as u32 + SERVER_ID_LIMIT, obj);
}
}
}
Expand All @@ -200,11 +199,11 @@ fn insert_in<Meta: ObjectMetadata>(store: &mut Vec<Option<Object<Meta>>>, object
match store.iter().position(|o| o.is_none()) {
Some(id) => {
store[id] = Some(object);
id as u32 + 1
id as u32
}
None => {
store.push(Some(object));
store.len() as u32
(store.len() - 1) as u32
}
}
}
Expand All @@ -215,7 +214,6 @@ fn insert_in_at<Meta: ObjectMetadata>(
id: usize,
object: Object<Meta>,
) -> Result<(), ()> {
let id = id - 1;
if id > store.len() {
Err(())
} else if id == store.len() {
Expand Down

0 comments on commit 738fc3b

Please sign in to comment.