HOWTO: Idiomatic Rust for callbacks with gtk (rust-gnome) - callback

I am currently learning Rust and looking to use it for developing a GUI
based application with GTK+. My problem relates to registering callbacks to
respond to GTK events/signals and mutating state within those callbacks.
I have a working but inelegant solution, so I would like to ask if there
is a cleaner, more idiomatic solution.
I have implemented my code as a struct with method implementations, where
the struct maintains references to the GTK widgets along with other state
that it needs. It constructs a closure that is passed to the
GtkWidget::connect* functions in order to receive events, draw to a
canvas, etc. This can cause problems with the borrow checker, as I will now
explain. I have some working but (IMHO) non-ideal code that I will
show.
Initial, non-working solution:
#![cfg_attr(not(feature = "gtk_3_10"), allow(unused_variables, unused_mut))]
extern crate gtk;
extern crate cairo;
use gtk::traits::*;
use gtk::signal::Inhibit;
use cairo::{Context, RectangleInt};
struct RenderingAPITestWindow {
window: gtk::Window,
drawing_area: gtk::DrawingArea,
width: i32,
height: i32
}
impl RenderingAPITestWindow {
fn new(width: i32, height: i32) -> RenderingAPITestWindow {
let window = gtk::Window::new(gtk::WindowType::TopLevel).unwrap();
let drawing_area = gtk::DrawingArea::new().unwrap();
drawing_area.set_size_request(width, height);
window.set_title("Cairo API test");
window.add(&drawing_area);
let instance = RenderingAPITestWindow{window: window,
drawing_area: drawing_area,
width: width,
height: height,
};
instance.drawing_area.connect_draw(|widget, cairo_context| {
instance.on_draw(cairo_context);
instance.drawing_area.queue_draw();
Inhibit(true)
});
instance.drawing_area.connect_size_allocate(|widget, rect| {
instance.on_size_allocate(rect);
});
instance.window.show_all();
return instance;
}
fn exit_on_close(&self) {
self.window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(true)
});
}
fn on_draw(&mut self, cairo_ctx: Context) {
cairo_ctx.save();
cairo_ctx.move_to(50.0, (self.height as f64) * 0.5);
cairo_ctx.set_font_size(18.0);
cairo_ctx.show_text("The only curse they could afford to put on a tomb these days was 'Bugger Off'. --PTerry");
cairo_ctx.restore();
}
fn on_size_allocate(&mut self, rect: &RectangleInt) {
self.width = rect.width as i32;
self.height = rect.height as i32;
}
}
fn main() {
gtk::init().unwrap_or_else(|_| panic!("Failed to initialize GTK."));
println!("Major: {}, Minor: {}", gtk::get_major_version(), gtk::get_minor_version());
let window = RenderingAPITestWindow::new(800, 500);
window.exit_on_close();
gtk::main();
}
The above fails to compile as the closures with
RenderingAPITestWindow::new that are created and passed to calls to
GtkWidget::connect* methods attempt to borrow instance. The
compiler states that the closures may outlive the function in which
they are declared and that instance is owned by the outer function,
hence the problem. Given that GTK may keep a reference to these closures
around for an unspecified amount of time, we need an approach in which the
lifetime can be determined at runtime, hence my next stab at the problem
in which the RenderingAPITestWindow instance is wrapped in
Rc<RefCell<...>>.
Wrapping the RenderingAPITestWindow instance compiles but dies at runtime:
#![cfg_attr(not(feature = "gtk_3_10"), allow(unused_variables, unused_mut))]
extern crate gtk;
extern crate cairo;
use std::rc::Rc;
use std::cell::RefCell;
use gtk::traits::*;
use gtk::signal::Inhibit;
use cairo::{Context, RectangleInt};
struct RenderingAPITestWindow {
window: gtk::Window,
drawing_area: gtk::DrawingArea,
width: i32,
height: i32
}
impl RenderingAPITestWindow {
fn new(width: i32, height: i32) -> Rc<RefCell<RenderingAPITestWindow>> {
let window = gtk::Window::new(gtk::WindowType::TopLevel).unwrap();
let drawing_area = gtk::DrawingArea::new().unwrap();
drawing_area.set_size_request(width, height);
window.set_title("Cairo API test");
window.add(&drawing_area);
let instance = RenderingAPITestWindow{window: window,
drawing_area: drawing_area,
width: width,
height: height,
};
let wrapped_instance = Rc::new(RefCell::new(instance));
let wrapped_instance_for_draw = wrapped_instance.clone();
wrapped_instance.borrow().drawing_area.connect_draw(move |widget, cairo_context| {
wrapped_instance_for_draw.borrow_mut().on_draw(cairo_context);
wrapped_instance_for_draw.borrow().drawing_area.queue_draw();
Inhibit(true)
});
let wrapped_instance_for_sizealloc = wrapped_instance.clone();
wrapped_instance.borrow().drawing_area.connect_size_allocate(move |widget, rect| {
wrapped_instance_for_sizealloc.borrow_mut().on_size_allocate(rect);
});
wrapped_instance.borrow().window.show_all();
return wrapped_instance;
}
fn exit_on_close(&self) {
self.window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(true)
});
}
fn on_draw(&mut self, cairo_ctx: Context) {
cairo_ctx.save();
cairo_ctx.move_to(50.0, (self.height as f64) * 0.5);
cairo_ctx.set_font_size(18.0);
cairo_ctx.show_text("The only curse they could afford to put on a tomb these days was 'Bugger Off'. --PTerry");
cairo_ctx.restore();
}
fn on_size_allocate(&mut self, rect: &RectangleInt) {
self.width = rect.width as i32;
self.height = rect.height as i32;
}
}
fn main() {
gtk::init().unwrap_or_else(|_| panic!("Failed to initialize GTK."));
println!("Major: {}, Minor: {}", gtk::get_major_version(), gtk::get_minor_version());
let wrapped_window = RenderingAPITestWindow::new(800, 500);
wrapped_window.borrow().exit_on_close();
gtk::main();
}
The above solution compiles but its not particularly pretty:
RenderingAPITestWindow::new returns an
Rc<RefCell<RenderingAPITestWindow>> rather than a
RenderingAPITestWindow
Accessing fields and methods of RenderingAPITestWindow is complicated
by the fact that the Rc<RefCell<...>> must be opened up; it now requires
wrapped_instance.borrow().some_method(...) rather than just
instance.some_method(...)
Each closure requires it's own clone of wrapped_instance; attempting
to use wrapped_instance would attempt to borrow an object -- the
wrapper rather than the RenderingAPITestWindow this time -- that is
owned by RenderingAPITestWindow::new as before
While the above compiles, it dies at runtime with:
thread '<main>' panicked at 'RefCell<T> already borrowed', ../src/libcore/cell.rs:442
An unknown error occurred
This is due to the fact that the call to window.show_all() causes GTK to
initialise the widget hierarchy, resulting in the drawing area widget
receiving a size-allocate event. Accessing the window to call
show_all() required that the Rc<RefCell<...>> is opened (hence
wrapped_instance.borrow().window.show_all();) and the instance
borrowed. Before the borrow ends when show_all() returns, GTK invokes the
drawing area's size-allocate event handler, which causes the closure
connected to it (4 lines above) to be invoked. The closure attempts to
borrow a mutable reference to the RenderingAPITestWindow instance
(wrapped_instance_for_sizealloc.borrow_mut().on_size_allocate(rect);)
in order to invoke the on_size_allocate method. This attempts to borrow a
mutable reference, while the first immutable reference is still in scope.
This second borrow causes the run-time panic.
The working but - IMHO - inelegant solution that I have managed to get
working so far is to split RenderingAPITestWindow into two structs, with
the mutable state that is to modified by the callbacks moved into a
separate struct.
Working but inelegant solution that splits the RenderingAPITestWindow struct:
#![cfg_attr(not(feature = "gtk_3_10"), allow(unused_variables, unused_mut))]
extern crate gtk;
extern crate cairo;
use std::rc::Rc;
use std::cell::RefCell;
use gtk::traits::*;
use gtk::signal::Inhibit;
use cairo::{Context, RectangleInt};
struct RenderingAPITestWindowState {
width: i32,
height: i32
}
impl RenderingAPITestWindowState {
fn new(width: i32, height: i32) -> RenderingAPITestWindowState {
return RenderingAPITestWindowState{width: width, height: height};
}
fn on_draw(&mut self, cairo_ctx: Context) {
cairo_ctx.save();
cairo_ctx.move_to(50.0, (self.height as f64) * 0.5);
cairo_ctx.set_font_size(18.0);
cairo_ctx.show_text("The only curse they could afford to put on a tomb these days was 'Bugger Off'. --PTerry");
cairo_ctx.restore();
}
fn on_size_allocate(&mut self, rect: &RectangleInt) {
self.width = rect.width as i32;
self.height = rect.height as i32;
}
}
struct RenderingAPITestWindow {
window: gtk::Window,
drawing_area: gtk::DrawingArea,
state: Rc<RefCell<RenderingAPITestWindowState>>
}
impl RenderingAPITestWindow {
fn new(width: i32, height: i32) -> Rc<RefCell<RenderingAPITestWindow>> {
let window = gtk::Window::new(gtk::WindowType::TopLevel).unwrap();
let drawing_area = gtk::DrawingArea::new().unwrap();
drawing_area.set_size_request(width, height);
window.set_title("Cairo API test");
window.add(&drawing_area);
let wrapped_state = Rc::new(RefCell::new(RenderingAPITestWindowState::new(width, height)))
;
let instance = RenderingAPITestWindow{window: window,
drawing_area: drawing_area,
state: wrapped_state.clone()
};
let wrapped_instance = Rc::new(RefCell::new(instance));
let wrapped_state_for_draw = wrapped_state.clone();
let wrapped_instance_for_draw = wrapped_instance.clone();
wrapped_instance.borrow().drawing_area.connect_draw(move |widget, cairo_context| {
wrapped_state_for_draw.borrow_mut().on_draw(cairo_context);
wrapped_instance_for_draw.borrow().drawing_area.queue_draw();
Inhibit(true)
});
let wrapped_state_for_sizealloc = wrapped_state.clone();
wrapped_instance.borrow().drawing_area.connect_size_allocate(move |widget, rect| {
wrapped_state_for_sizealloc.borrow_mut().on_size_allocate(rect);
});
wrapped_instance.borrow().window.show_all();
return wrapped_instance;
}
fn exit_on_close(&self) {
self.window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(true)
});
}
}
fn main() {
gtk::init().unwrap_or_else(|_| panic!("Failed to initialize GTK."));
println!("Major: {}, Minor: {}", gtk::get_major_version(), gtk::get_minor_version());
let wrapped_window = RenderingAPITestWindow::new(800, 500);
wrapped_window.borrow().exit_on_close();
gtk::main();
}
While the above code works as required, I would like to find a better way
for going forward; I would like to ask if anyone knows a better approach as
the above complicates the programming process a fair bit, with the need to
use Rc<RefCell<...>> and split structs to satisfy Rust's borrowing rules.

Here's a working version that I came up with:
#![cfg_attr(not(feature = "gtk_3_10"), allow(unused_variables, unused_mut))]
extern crate gtk;
extern crate cairo;
use std::rc::Rc;
use std::cell::RefCell;
use gtk::traits::*;
use gtk::signal::Inhibit;
use cairo::{Context, RectangleInt};
struct RenderingAPITestWindow {
window: gtk::Window,
drawing_area: gtk::DrawingArea,
state: RefCell<RenderingState>,
}
struct RenderingState {
width: i32,
height: i32,
}
impl RenderingAPITestWindow {
fn new(width: i32, height: i32) -> Rc<RenderingAPITestWindow> {
let window = gtk::Window::new(gtk::WindowType::TopLevel).unwrap();
let drawing_area = gtk::DrawingArea::new().unwrap();
drawing_area.set_size_request(width, height);
window.set_title("Cairo API test");
window.add(&drawing_area);
let instance = Rc::new(RenderingAPITestWindow {
window: window,
drawing_area: drawing_area,
state: RefCell::new(RenderingState {
width: width,
height: height,
}),
});
{
let instance2 = instance.clone();
instance.drawing_area.connect_draw(move |widget, cairo_context| {
instance2.state.borrow().on_draw(cairo_context);
instance2.drawing_area.queue_draw();
Inhibit(true)
});
}
{
let instance2 = instance.clone();
instance.drawing_area.connect_size_allocate(move |widget, rect| {
instance2.state.borrow_mut().on_size_allocate(rect);
});
}
instance.window.show_all();
instance
}
fn exit_on_close(&self) {
self.window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(true)
});
}
}
impl RenderingState {
fn on_draw(&self, cairo_ctx: Context) {
cairo_ctx.save();
cairo_ctx.move_to(50.0, (self.height as f64) * 0.5);
cairo_ctx.set_font_size(18.0);
cairo_ctx.show_text("The only curse they could afford to put on a tomb these days was 'Bugger Off'. --PTerry");
cairo_ctx.restore();
}
fn on_size_allocate(&mut self, rect: &RectangleInt) {
self.width = rect.width as i32;
self.height = rect.height as i32;
}
}
fn main() {
gtk::init().unwrap_or_else(|_| panic!("Failed to initialize GTK."));
println!("Major: {}, Minor: {}", gtk::get_major_version(), gtk::get_minor_version());
let window = RenderingAPITestWindow::new(800, 500);
window.exit_on_close();
gtk::main();
}
I arrived at this through a few observations:
The instance is being shared across multiple closures for an undetermined amount of time. Rc is the right answer to that scenario because it provides shared ownership. Rc is very ergonomic to use; it works like any other pointer type.
The only part of instance that is actually mutated is your state. Since your instance is being shared, it cannot be borrowed mutably using the standard &mut pointer. Therefore, you must use interior mutability. This is what RefCell provides. Note though, that you only need to use RefCell on the state you're mutating. So this still separates out the state into a separate struct, but it works nicely IMO.
A possible modification to this code is to add #[derive(Clone, Copy)] to the definition of the RenderingState struct. Since it can be Copy (because all of its component types are Copy), you can use Cell instead of RefCell.

Related

SwiftUI previews fails with __designTimeString error

My application builds successfully and runs fine, but none of the previews will work. The error just says "Failed to build" and there's an option to see the diagnostics. The diagnostics say the following:
global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'
----------------------------------------
CompileDylibError: Failed to build GameCellView.swift
Compiling failed: global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'
/Users/Starx/Documents/Code/Swift/Shobu/Shobu/Views/GameCellView.swift:45:49: error: global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'
DragGesture(coordinateSpace: .named(__designTimeString("#5282.[2].[6].property.[0].[0].arg[0].value.[1].[0].[0].modifier[3].arg[0].value.arg[0].value.arg[0].value", fallback: "BoardCoordinates")))
^
SwiftUI.__designTimeString:1:13: note: where 'T' = 'AnyHashable'
public func __designTimeString<T>(_ key: String, fallback: T) -> T where T : ExpressibleByStringLiteral
The drag gesture it points to as the source of the error is the following:
.gesture(
DragGesture(coordinateSpace: .named("BoardCoordinates"))
.onChanged {point in
guard CanDrag else {
return
}
dragAmount = CGSize(width: point.translation.width, height: point.translation.height)
dragState = GetDragState(Point: point.location)
}
.onEnded {point in
CellDropped(Point: point.location)
dragAmount = .zero
dragState = .unknown
}
)
Any ideas what's causing this? As I said, the App functions, but it would be nice to not have to rebuild every time I'm adjusting UI spacing or something like that.
The issue for me was identical. Turned out to be the literal string in the .named() coordinate space for me.
If you create the string first with
let coordinateSpaceName: String = "BoardCoordinates"
and then use:
.gesture(
DragGesture(coordinateSpace: .named(coordinateSpaceName))
.onChanged {point in
guard CanDrag else {
return
}
dragAmount = CGSize(width: point.translation.width, height: point.translation.height)
dragState = GetDragState(Point: point.location)
}
.onEnded {point in
CellDropped(Point: point.location)
dragAmount = .zero
dragState = .unknown
}
)
that should ensure that string conforms to ExpressibleByStringLiteral.
Why a literal one does not I haven't figured out yet.

why mutating method applied on instance doesn't save changes? swift

I extend my custom protocol with mutable function. And then apply it on the instance of the corresponding type. But instance is changed only on that row. On the next row, it has previous value. Why changes made on the instance doesn't persist?
If I assign the result of mutating to the var/let. Then the result saves. Or if I apply harderWorkout() in the print() statement it print changed value.
struct Workout {
var distance: Double
var time: Double
var averageHR: Int
}
extension Workout: CustomStringConvertible {
var description: String {
return "Workout(distance: \(distance), time: \(time), averageHR: \(averageHR)"
}
}
extension Workout {
mutating func harderWorkout() -> Workout {
return Workout(distance: (self.distance * 2), time: (self.time * 2), averageHR: (self.averageHR + 40))
}
}
var workout = Workout(distance: 500, time: 50, averageHR: 100)
print(workout) //Workout(distance: 500.0, time: 50.0, averageHR: 100, Speed: 10.0
workout.harderWorkout()
print(workout) //Workout(distance: 500.0, time: 50.0, averageHR: 100, Speed: 10.0
In the last print I expected to see Workout(distance: 1000.0, time: 100.0, averageHR: 140 but it's not clear to me why harderWorkout() method doesn't change the workout instance. Maybe it's because of the value type. But I put the mutable prefix...
Will be very thankful if someone explains to me the reason and its mechanism.
Instead of returning Workout instance from harderWorkout() method, assign the new Workout instance to self, i.e.
extension Workout {
mutating func harderWorkout() {
self = Workout(distance: (self.distance * 2), time: (self.time * 2), averageHR: (self.averageHR + 40))
}
}
Alternatively, you can simply change the distance, time and averageHR values of the same instance, i.e.
extension Workout {
mutating func harderWorkout() {
self.distance *= 2
self.time *= 2
self.averageHR += 40
}
}
It is pretty, simple - in your workoutHarder() you create a new Workout and return that, instead of mutating it.
If you expect it to mutate, you will need to to the following:
extension Workout {
mutating func harderWorkout() -> Workout {
self.distance *= 2
self.time *=2
self.averageHR += 40
return self
}
}
You see that it now returns from self, and maybe the method dont need to return at all if you just want it to mutate?

What's the correct way to implement the equivalent of multiple mutable (statically allocated, statically dispatched, etc.) callbacks in Rust?

I have the following example code, which is the standard basis of event-driven APIs in other programming languages, but in Rust the borrow checker blocks it with "cannot borrow p1 as mutable more than once at a time":
struct Pen {
color_cmyk: u32,
ink: usize,
}
impl Pen {
pub fn new() -> Pen {
Pen {
color_cmyk: 0x80800000,
ink: 20000,
}
}
pub fn write(&mut self, text: &str) -> bool {
if self.ink < text.len() {
return false;
}
self.ink -= text.len();
true
}
}
fn main() {
println!("Hello, world !");
let mut p1 = Pen::new();
p1.write("Hello");
println!("ink: {}, color: {}", p1.ink, p1.color_cmyk);
let mut cb = |text| if p1.write(text) {
println!("{}", text);
} else {
println!("Out of ink !");
};
let mut cb2 = |text| {
p1.write(text);
p1.ink
};
cb("Hello");
cb("World");
println!("{}", cb2("Hello"));
}
error[E0499]: cannot borrow `p1` as mutable more than once at a time
--> src/main.rs:37:23
|
31 | let mut cb = |text| if p1.write(text) {
| ------ -- previous borrow occurs due to use of `p1` in closure
| |
| first mutable borrow occurs here
...
37 | let mut cb2 = |text| {
| ^^^^^^ second mutable borrow occurs here
38 | p1.write(text);
| -- borrow occurs due to use of `p1` in closure
...
45 | }
| - first borrow ends here
The code can be used, for example, to implement two callbacks to a window: one for handling keyboard events and another for handling mouse events, both of which update the window state (ex: changing color, closing the window, etc.).
I know that this question appears elsewhere in Stack Overflow and other forums, but in general, the answers focus on describing the reason of the problem and rarely propose a complete general solution for it:
Cannot borrow `x` as mutable more than once at a time
How to bypass “cannot borrow as mutable more than once”?
Cannot borrow as mutable more than once at a time
Passing mutable context into callbacks
Creating a callback system using closures
Execute callbacks like as mutable borrowing from cycle
Callback to mutable self
One way is to use a RefCell, which allows you to mutate things with only &Pen instead of &mut Pen, at the cost of pushing the borrow-checking to runtime. It’s very cheap: there is no allocation, just a single flag test.
The main downside is that violating the rules will result in a panic at runtime. A useful rule of thumb is to never borrow for any longer than necessary (think of them as “single-threaded mutexes”).
use std::cell::RefCell;
fn main() {
println!("Hello, world !");
let p1 = RefCell::new(Pen::new());
{
let mut rp1 = p1.borrow_mut();
rp1.write("Hello");
println!("ink: {}, color: {}", rp1.ink, rp1.color_cmyk);
}
let cb = |text| {
if p1.borrow_mut().write(text) {
println!("{}", text);
}
else {
println!("Out of ink !");
}
};
let cb2 = |text| {
let mut rp1 = p1.borrow_mut();
rp1.write(text);
rp1.ink
};
cb("Hello");
cb("World");
println!("{}", cb2("Hello"));
}
Another way is to set up the callback system to pass in the object that you’re modifying as an argument. The trade-off is then your callback system needs to be aware of this state.
fn main() {
println!("Hello, world !");
let mut p1 = Pen::new();
p1.write("Hello");
println!("ink: {}, color: {}", p1.ink, p1.color_cmyk);
let cb = |p1: &mut Pen, text| if p1.write(text) {
println!("{}", text);
} else {
println!("Out of ink !");
};
let cb2 = |p1: &mut Pen, text| {
p1.write(text);
p1.ink
};
cb(&mut p1, "Hello");
cb(&mut p1, "World");
println!("{}", cb2(&mut p1, "Hello"));
}

Compiler choosing the wrong initializer

I have an EXC_BAD_ACCESS(code=2 ...) error that keeps popping up that I need some help with. I believe I've managed to pin down the source of the malformed pointer, but I'm at a loss as to how to fix it.
Apparently the swift compiler is choosing the wrong initializer for one of my classes. According to Instruments, sometimes when the class Description is initialized, it calls the initializer for LyricBlock. Not all the time, just sometimes. It does this regardless of whether the compiler is set to -Onone or -O whole-module-optimization.
Here's what the two classes look like:
class LyricBlock: Node, LeftDelimited, RightDelimited {
var leftDelimiter: Delimiter
var rigthDelimiter: Delimiter
init(start: Int, body: Range<Int>, end: Int) {
self.leftDelimiter = Delimiter(range: start..<body.lowerBound)
self.rightDelimiter = Delimiter(range: body.upperBound..<end)
super.init(range: body)
}
}
class Description: Node, LeftDelimited, RightDelimited {
var leftDelimiter: Delimiter
var leftDelimiter: Delimiter
init(start: Int, body: Range<Int>, end: Int) {
self.leftDelimiter = Delimiter(range: start..<body.lowerBound)
self.rightDelimiter = Delimiter(range: body.upperBound..<end)
super.init(range: body)
}
}
As you can see, LyricBlock and Description inherit from a Node base class and share a couple protocols in common, but otherwise they have nothing to do with each other.
Some possibly relevant code:
class Node {
weak var parent: Node?
var next: Node?
var firstChild: Node?
weak var lastChild: Node?
let offset: Int
internal(set) var length: Int
init(range: Range<Int>) {
self.offset = range.lowerBound
self.length = range.upperBound - range.lowerBound
}
func addChild(_ child: Node) {
if firstChild == nil {
firstChild = child
} else {
lastChild?.next = child
}
lastChild = child
child.parent = self
}
}
class Parser {
// ...
func processLine(in buffer: Buffer) {
// Parse the current line as a block node.
var block = blockForLine(in: buffer)
// Try to find an appropriate container node. If none can be found, block will be replaced with Description.
let container = appropriateContainer(for: &block, in: buffer)
container.addChild(block)
// Edge case to parse first-line lyrics
if let cueBlock = block as? CueBlock {
if let lyricBlock = scanForLyric(in: buffer, at: cueBlock.direction.range.lowerBound) {
let lyricContainer = LyricContainer(range: lyricBlock.range.lowerBound..<endOfLineCharNumber)
lyricContainer.addChild(lyricBlock)
cueBlock.replaceDirection(with: lyricContainer)
parseInlines(for: lyricBlock, in: buffer)
}
}
// Parse inlines as appropriate
switch block {
case is FacsimileBlock, is Description, is LyricBlock:
parseInlines(for: block, in: buffer)
// ...
}
}
func blockForLine(in buffer: Buffer) -> Node {
let whitespace = buffer.scanForFirstNonspace(at: charNumber, limit: endOfLineCharNumber)
// ...
let endWhitespace = buffer.scanBackwardForFirstNonspace(at: endOfLineCharNumber, limit: wc)
let description = Description(start: charNumber, body: whitespace..< endWhitespace, end: endOfLineCharNumber)
return description
}
func appropriateContainer(for block: inout Node, in buffer: Buffer) -> Node {
switch block {
// These block types can only ever be level-1
case is Header, is Description, is EndBlock, is HorizontalBreak:
return root
// ...
case is LyricBlock:
guard let cueContainer = root.lastChild as? CueContainer else { break }
guard let cueBlock = cueContainer.lastChild as? CueBlock else { break }
guard let direction = cueBlock.direction as? LyricContainer else { break }
direction.extendLengthToInclude(node: block)
cueBlock.extendLengthToInclude(node: direction)
cueContainer.extendLengthToInclude(node: cueBlock)
return direction
default:
break
}
let whitespace = buffer.scanForFirstNonspace(at: charNumber, limit: endOfLineCharNumber)
let endWhitespace = buffer.scanBackwardForFirstNonspace(at: endOfLineCharNumber, limit: wc)
// Invalid syntax, time to fail gracefully
block = Description(start: charNumber, body: whitespace..< endWhitespace, end: endOfLineCharNumber)
return root
}
func parseInlines(for stream: Node, in buffer: Buffer) {
// ... scans buffer for inlines and enques them in queue
while let next = queue.dequeue() {
let nextRange = next.rangeIncludingMarkers
if nextRange.lowerBound > j {
let lit = Literal(range: j..<nextRange.lowerBound)
stream.addChild(lit)
}
stream.addChild(next)
j = nextRange.upperBound
}
if j < endOfLineCharNumber {
let lit = Literal(range: j..<nodeRange.upperBound)
stream.addChild(lit)
}
}
// ...
}
As a side note, I wondered if I might be running into a mangling issue with the class signatures and tried making rightDelimiter and leftDelimiter properties of Node instead of using protocols. This resulted in the compiler calling my Identifier initializer instead. I don't know what that proves. Frankly I'm at a loss. Help?
After some more tinkering I was able to at least partially solve this problem.
It turns out that, for whatever reason, ARC craps out on linked lists above a certain size. I was able to confirm this by creating a generic linked list in a new project and slowly stress testing it with progressively longer lists.
class LLNode<T> {
var value: T?
var next: LLNode<T>?
weak var previous: LLNode<T>?
}
class LList<T> {
var head: LLNode<T>
var tail: LLNode<T>
// boring old linked list implementation
}
let list = List<Int>()
for i in 0..<10000 {
list.append(i)
}
Sure enough, after about 10,000 nodes I started getting EXC_BAD_ACCESS again on the generic list, but only when list was deinitialized. This matched the behavior I was getting exactly with the parser above. Since my code uses linked lists to model children in a tree (two dimensions of reference counting!), ARC was having to resolve all those references on its own -- and failing. Why it crashes like that is still beyond my ability to explain, but that at least explains the source of the crash.
To confirm, I created another linked list in C and just made a Swift wrapper around it, with all of the garbage collection being implemented in C.
import liblist
class List<T> {
var list: UnsafeMutablePointer<llist>
// Swift interface with list
deinit {
list_free(list)
}
}
The wrapper was able to handle every size I threw at it -- as much as 500,000 nodes, at which point I felt satisfied and stopped testing.
If anyone is interested in the full code I used to get around this problem, I've created a Swift package at https://github.com/dmcarth/List

How to change the value of a child from a Mirror introspection

I'm doing a bunch of BLE in iOS, which means lots of tight packed C structures being encoded/decoded as byte packets. The following playground snippets illustrate what I'm trying to do generically.
import Foundation
// THE PROBLEM
struct Thing {
var a:UInt8 = 0
var b:UInt32 = 0
var c:UInt8 = 0
}
sizeof(Thing) // --> 9 :(
var thing = Thing(a: 0x42, b: 0xDEADBEAF, c: 0x13)
var data = NSData(bytes: &thing, length: sizeof(Thing)) // --> <42000000 afbeadde 13> :(
So given a series of fields of varying size, we don't get the "tightest" packing of bytes. Pretty well known and accepted. Given my simple structs, I'd like to be able to arbitrarily encode the fields back to back with no padding or alignment stuff. Relatively easy actually:
// ARBITRARY PACKING
var mirror = Mirror(reflecting: thing)
var output:[UInt8] = []
mirror.children.forEach { (label, child) in
switch child {
case let value as UInt32:
(0...3).forEach { output.append(UInt8((value >> ($0 * 8)) & 0xFF)) }
case let value as UInt8:
output.append(value)
default:
print("Don't know how to serialize \(child.dynamicType) (field \(label))")
}
}
output.count // --> 6 :)
data = NSData(bytes: &output, length: output.count) // --> <42afbead de13> :)
Huzzah! Works as expected. Could probably add a Class around it, or maybe a Protocol extension and have a nice utility. The problem I'm up against is the reverse process:
// ARBITRARY DEPACKING
var input = output.generate()
var thing2 = Thing()
"\(thing2.a), \(thing2.b), \(thing2.c)" // --> "0, 0, 0"
mirror = Mirror(reflecting:thing2)
mirror.children.forEach { (label, child) in
switch child {
case let oldValue as UInt8:
let newValue = input.next()!
print("new value for \(label!) would be \(newValue)")
// *(&child) = newValue // HOW TO DO THIS IN SWIFT??
case let oldValue as UInt32: // do little endian
var newValue:UInt32 = 0
(0...3).forEach {
newValue |= UInt32(input.next()!) << UInt32($0 * 8)
}
print("new value for \(label!) would be \(newValue)")
// *(&child) = newValue // HOW TO DO THIS IN SWIFT??
default:
print("skipping field \(label) of type \(child.dynamicType)")
}
}
Given an unpopulated struct value, I can decode the byte stream appropriately, figure out what the new value would be for each field. What I don't know how to do is to actually update the target struct with the new value. In my example above, I show how I might do it with C, get the pointer to the original child, and then update its value with the new value. I could do it easily in Python/Smalltalk/Ruby. But I don't know how one can do that in Swift.
UPDATE
As suggested in comments, I could do something like the following:
// SPECIFIC DEPACKING
extension GeneratorType where Element == UInt8 {
mutating func _UInt8() -> UInt8 {
return self.next()!
}
mutating func _UInt32() -> UInt32 {
var result:UInt32 = 0
(0...3).forEach {
result |= UInt32(self.next()!) << UInt32($0 * 8)
}
return result
}
}
extension Thing {
init(inout input:IndexingGenerator<[UInt8]>) {
self.init(a: input._UInt8(), b: input._UInt32(), c: input._UInt8())
}
}
input = output.generate()
let thing3 = Thing(input: &input)
"\(thing3.a), \(thing3.b), \(thing3.c)" // --> "66, 3735928495, 19"
Basically, I move the various stream decoding methods to byte stream (i.e. GeneratorType where Element == UInt8), and then I just have to write an initializer that strings those off in the same order and type the struct is defined as. I guess that part, which is essentially "copying" the structure definition itself (and therefore error prone), is what I had hoped to use some sort of introspection to handle. Mirrors are the only real Swift introspection I'm aware of, and it seems pretty limited.
As discussed in the comments, I suspect this is over-clever. Swift includes a lot of types not friendly to this approach. I would focus instead on how to make the boilerplate as easy as possible, without worrying about eliminating it. For example, this is very sloppy, but is in the direction I would probably go:
Start with some helper packer/unpacker functions:
func pack(values: Any...) -> [UInt8]{
var output:[UInt8] = []
for value in values {
switch value {
case let i as UInt32:
(0...3).forEach { output.append(UInt8((i >> ($0 * 8)) & 0xFF)) }
case let i as UInt8:
output.append(i)
default:
assertionFailure("Don't know how to serialize \(value.dynamicType)")
}
}
return output
}
func unpack<T>(bytes: AnyGenerator<UInt8>, inout target: T) throws {
switch target {
case is UInt32:
var newValue: UInt32 = 0
(0...3).forEach {
newValue |= UInt32(bytes.next()!) << UInt32($0 * 8)
}
target = newValue as! T
case is UInt8:
target = bytes.next()! as! T
default:
// Should throw an error here probably
assertionFailure("Don't know how to deserialize \(target.dynamicType)")
}
}
Then just call them:
struct Thing {
var a:UInt8 = 0
var b:UInt32 = 0
var c:UInt8 = 0
func encode() -> [UInt8] {
return pack(a, b, c)
}
static func decode(bytes: [UInt8]) throws -> Thing {
var thing = Thing()
let g = anyGenerator(bytes.generate())
try unpack(g, target: &thing.a)
try unpack(g, target: &thing.b)
try unpack(g, target: &thing.c)
return thing
}
}
A little more thought might be able to make the decode method a little less repetitive, but this is still probably the way I would go, explicitly listing the fields you want to encode rather than trying to introspect them. As you note, Swift introspection is very limited, and it may be that way for a long time. It's mostly used for debugging and logging, not logic.
I have tagged Rob's answer is the official answer. But I'd thought I'd share what I ended up doing as well, inspired by the comments and answers.
First, I fleshed out my "Problem" a little to include a nested structure:
struct Inner {
var ai:UInt16 = 0
var bi:UInt8 = 0
}
struct Thing {
var a:UInt8 = 0
var b:UInt32 = 0
var inner = Inner()
var c:UInt8 = 0
}
sizeof(Thing) // --> 12 :(
var thing = Thing(a: 0x42, b: 0xDEADBEAF, inner: Inner(ai: 0x1122, bi: 0xDD), c: 0x13)
var data = NSData(bytes: &thing, length: sizeof(Thing)) // --> <42000000 afbeadde 2211dd13> :(
For Arbitrary Packing, I stuck with the same generic approach:
protocol Packable {
func packed() -> [UInt8]
}
extension UInt8:Packable {
func packed() -> [UInt8] {
return [self]
}
}
extension UInt16:Packable {
func packed() -> [UInt8] {
return [(UInt8((self >> 0) & 0xFF)), (UInt8((self >> 8) & 0xFF))]
}
}
extension UInt32:Packable {
func packed() -> [UInt8] {
return [(UInt8((self >> 0) & 0xFF)), (UInt8((self >> 8) & 0xFF)), (UInt8((self >> 16) & 0xFF)), (UInt8((self >> 24) & 0xFF))]
}
}
extension Packable {
func packed() -> [UInt8] {
let mirror = Mirror(reflecting:self)
var bytes:[UInt8] = []
mirror.children.forEach { (label, child) in
switch child {
case let value as Packable:
bytes += value.packed()
default:
print("Don't know how to serialize \(child.dynamicType) (field \(label))")
}
}
return bytes
}
}
Being able to "pack" things is as easy adding them to the Packable protocol and telling them to pack themselves. For my cases above, I only need 3 different types of signed integers, but one could add lots more. For example, in my own code, I have some Enums derived from UInt8 which I added the packed method to.
extension Thing:Packable { }
extension Inner:Packable { }
var output = thing.packed()
output.count // --> 9 :)
data = NSData(bytes: &output, length: output.count) // --> <42afbead de2211dd 13> :)
To be able to unpack stuff, I came up with a little bit of support:
protocol UnpackablePrimitive {
static func unpack(inout input:IndexingGenerator<[UInt8]>) -> Self
}
extension UInt8:UnpackablePrimitive {
static func unpack(inout input:IndexingGenerator<[UInt8]>) -> UInt8 {
return input.next()!
}
}
extension UInt16:UnpackablePrimitive {
static func unpack(inout input:IndexingGenerator<[UInt8]>) -> UInt16 {
return UInt16(input.next()!) | (UInt16(input.next()!) << 8)
}
}
extension UInt32:UnpackablePrimitive {
static func unpack(inout input:IndexingGenerator<[UInt8]>) -> UInt32 {
return UInt32(input.next()!) | (UInt32(input.next()!) << 8) | (UInt32(input.next()!) << 16) | (UInt32(input.next()!) << 24)
}
}
With this, I can then add initializers to my high level structures, e.g.
extension Inner:Unpackable {
init(inout packed bytes:IndexingGenerator<[UInt8]>) {
self.init(ai: UInt16.unpack(&bytes), bi: UInt8.unpack(&bytes))
}
}
extension Thing:Unpackable {
init(inout packed bytes:IndexingGenerator<[UInt8]>) {
self.init(a: UInt8.unpack(&bytes), b: UInt32.unpack(&bytes), inner: Inner(packed:&bytes), c: UInt8.unpack(&bytes))
}
}
What I liked about this is that these initializers call the default initializer in the same order and types as the structure is defined. So if the structure changes in type or order, I have to revisit the (packed:) initializer. The kids a bit long, but not too.
What I didn't like about this, was having to pass the inout everywhere. I'm honestly not sure what the value is of value based generators, since passing them around you almost always want to share state. Kind of the whole point of reifying an object that captures the position of a stream of data, is to be able to share it. I also don't like having to specify IndexingGenerator directly, but I imagine there's some fu magic that would make that less specific and still work, but I'm not there yet.
I did play with something more pythonic, where I return a tuple of the type and the remainder of a passed array (rather than a stream/generator), but that wasn't nearly as easy to use at the top level init level.
I also tried putting the static methods as extensions on byte based generators, but you have to use a function (would rather have used a computed var with side effects) there whose name doesn't match a type, so you end up with something like
self.init(a: bytes._UInt8(), b: bytes._UInt32(), inner: Inner(packed:&bytes), c: bytes._UInt8())
This is shorter, but doesn't put the type like functions next to the argument names. And would require all kinds of application specific method names to be added as well as one extended the set of UnpackablePrimitives.