Skip to content

Commit

Permalink
manual cleanup Copy derive: gam crate
Browse files Browse the repository at this point in the history
There isn't a simple regex to deal with the loss of Copy when
deprecating xous_ipc, so this has to be a manual pass.

Losing the copy trait means (maybe) some loss of efficiency
in the API because we have to clone some structures instead
of copying them, although, I think actually what might have
been happening under the hood anyways is the copy was implemented
as a clone-under-the-hood for the xous-ipc.

Either way, this happens in the high level management structures
for modals, so it's not the end of the world to incur a clone
on the description of the structures -- we'd have to pay
more attention if this happened e.g. at the pixel shuffling
level, but a few UI strings at human-interaction rates
isn't going to kill us.
  • Loading branch information
bunnie committed Sep 22, 2024
1 parent f8719d7 commit 58527ea
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion services/gam/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub(crate) struct GamActivation {
pub(crate) result: Option<ActivationResult>,
}

#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone)]
pub(crate) struct MenuManagement {
pub(crate) item: MenuItem,
pub(crate) op: MenuMgrOp,
Expand Down
2 changes: 1 addition & 1 deletion services/gam/src/api/rkyv_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub(crate) enum Return {
NotCurrentlyDrawable,
}

#[derive(Debug, Eq, PartialEq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[derive(Debug, Eq, PartialEq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone)]
pub(crate) enum MenuMgrOp {
// incoming is one of these ops
AddItem,
Expand Down
10 changes: 5 additions & 5 deletions services/gam/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<'a> Menu<'a> {
// something)
pub fn delete_item(&mut self, item: &str) -> bool {
let len_before = self.items.len();
self.items.retain(|&candidate| candidate.name.as_str() != item);
self.items.retain(|candidate| candidate.name.as_str() != item);

// now, recompute the height
let mut total_items = self.num_items();
Expand All @@ -208,7 +208,7 @@ impl<'a> Menu<'a> {
use core::fmt::Write;
let canvas_size = self.gam.get_canvas_bounds(self.canvas).unwrap();

let item = self.items[index as usize];
let item = &self.items[index as usize];
let mut item_tv = TextView::new(
self.canvas,
TextBounds::BoundingBox(Rectangle::new(
Expand Down Expand Up @@ -357,7 +357,7 @@ impl<'a> Menu<'a> {
log::debug!("got key '{}'", k);
match k {
'∴' => {
let mi = self.items[self.index];
let mi = &self.items[self.index];
// give up focus before issuing the command, as some commands conflict with loss of
// focus...
if mi.close_on_select {
Expand Down Expand Up @@ -562,12 +562,12 @@ pub fn menu_matic(
.expect("menu manager received unexpected message type");
match mgmt.op {
MenuMgrOp::AddItem => {
menu.lock().unwrap().add_item(mgmt.item);
menu.lock().unwrap().add_item(mgmt.item.clone());
mgmt.op = MenuMgrOp::Ok;
buffer.replace(mgmt).unwrap();
}
MenuMgrOp::InsertItem(at) => {
if menu.lock().unwrap().insert_item(mgmt.item, at) {
if menu.lock().unwrap().insert_item(mgmt.item.clone(), at) {
mgmt.op = MenuMgrOp::Ok;
buffer.replace(mgmt).unwrap();
} else {
Expand Down
4 changes: 2 additions & 2 deletions services/gam/src/modal/checkboxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ impl ActionApi for CheckBoxes {
self.gam.relinquish_focus().unwrap();
xous::yield_slice();

let buf =
Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload");
let buf = Buffer::into_buf(self.action_payload.clone())
.expect("couldn't convert message to payload");
buf.send(self.action_conn, self.action_opcode)
.map(|_| ())
.expect("couldn't send action message");
Expand Down
7 changes: 4 additions & 3 deletions services/gam/src/modal/consoleinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Clone for ConsoleInput {
ConsoleInput {
action_conn: self.action_conn,
action_opcode: self.action_opcode,
action_payload: self.action_payload,
action_payload: self.action_payload.clone(),
gam: crate::Gam::new(&xous_names::XousNames::new().unwrap()).unwrap(),
}
}
Expand Down Expand Up @@ -48,15 +48,16 @@ impl ActionApi for ConsoleInput {
self.gam.relinquish_focus().unwrap();
xous::yield_slice();

let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload");
let buf = Buffer::into_buf(self.action_payload.clone())
.expect("couldn't convert message to payload");
buf.send(self.action_conn, self.action_opcode)
.map(|_| ())
.expect("couldn't send action message");
return None;
}
_ => {
// text entry
self.action_payload.content.push(k).expect("ran out of space storing password");
self.action_payload.content.push(k);
log::trace!("****update payload: {}", self.action_payload.content);
}
}
Expand Down
9 changes: 8 additions & 1 deletion services/gam/src/modal/progressbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ impl<'a, 'b> ProgressBar<'a, 'b> {
if new_percent != self.current_progress_percent {
log::debug!("progress: {}", new_percent);
self.slider.set_state(new_percent);
self.modal.modify(Some(crate::ActionType::Slider(*self.slider)), None, false, None, false, None);
self.modal.modify(
Some(crate::ActionType::Slider(self.slider.clone())),
None,
false,
None,
false,
None,
);
self.modal.redraw(); // stage the modal box pixels to the back buffer
xous::yield_slice(); // this gives time for the GAM to do the sending
self.current_progress_percent = new_percent;
Expand Down
4 changes: 2 additions & 2 deletions services/gam/src/modal/radiobuttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ impl ActionApi for RadioButtons {
self.gam.relinquish_focus().unwrap();
xous::yield_slice();

let buf =
Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload");
let buf = Buffer::into_buf(self.action_payload.clone())
.expect("couldn't convert message to payload");
buf.send(self.action_conn, self.action_opcode)
.map(|_| ())
.expect("couldn't send action message");
Expand Down
4 changes: 2 additions & 2 deletions services/gam/src/modal/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::*;
// the enum_actions API to update the progress state in an efficient manner.
// Thus it does not include its own GAM reference; instead we create one on
// the fly when needed.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Clone)]
pub struct Slider {
pub min: u32,
pub max: u32,
Expand Down Expand Up @@ -132,7 +132,7 @@ impl ActionApi for Slider {
// estimate width of current setting
tv.bounds_computed = None;
tv.bounds_hint = TextBounds::GrowableFromTl(Point::new(0, 0), maxwidth);
write!(tv, "{}{}", self.action_payload, self.units.to_str()).unwrap();
write!(tv, "{}{}", self.action_payload, &self.units).unwrap();
modal.gam.bounds_compute_textview(&mut tv).expect("couldn't simulate text size");
let textwidth = if let Some(bounds) = tv.bounds_computed {
bounds.br.x - bounds.tl.x
Expand Down
14 changes: 9 additions & 5 deletions services/gam/src/modal/textentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub type ValidatorErr = String;

pub type Payloads = [TextEntryPayload; MAX_FIELDS as usize];

#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone, Eq, PartialEq, Default)]
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone, Eq, PartialEq, Default)]
pub struct TextEntryPayloads(Payloads, usize);

impl TextEntryPayloads {
pub fn first(&self) -> TextEntryPayload { self.0[0] }
pub fn first(&self) -> TextEntryPayload { self.0[0].clone() }

pub fn content(&self) -> Vec<TextEntryPayload> { self.0[..self.1].to_vec() }
}
Expand Down Expand Up @@ -623,7 +623,7 @@ impl ActionApi for TextEntry {
}
}
if let Some(validator) = self.validator {
if let Some(err_msg) = validator(*payload, self.action_opcode) {
if let Some(err_msg) = validator(payload.clone(), self.action_opcode) {
payload.content.clear(); // reset the input field
return Some(err_msg);
}
Expand All @@ -646,8 +646,12 @@ impl ActionApi for TextEntry {

let mut payloads: TextEntryPayloads = Default::default();
payloads.1 = self.max_field_amount as usize;
payloads.0[..self.max_field_amount as usize]
.copy_from_slice(&self.action_payloads[..self.max_field_amount as usize]);
for (dst, src) in payloads.0[..self.max_field_amount as usize]
.iter_mut()
.zip(self.action_payloads[..self.max_field_amount as usize].iter())
{
*dst = src.clone();
}
let buf = Buffer::into_buf(payloads).expect("couldn't convert message to payload");
buf.send(self.action_conn, self.action_opcode)
.map(|_| ())
Expand Down

0 comments on commit 58527ea

Please sign in to comment.