Skip to content

Commit

Permalink
Fix soundness of all app borrows.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Jul 19, 2024
1 parent f23aec7 commit bbe949e
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 123 deletions.
48 changes: 34 additions & 14 deletions examples/draw/draw.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
use nannou::prelude::*;
use std::any::Any;

fn main() {
nannou::sketch(view).run()
}

fn view(app: &App) {
// Begin drawing
let draw = app.draw();
draw.background().color(WHITE);

// Clear the background to blue.
draw.background().color(CORNFLOWER_BLUE);

// Draw a purple triangle in the top left half of the window.
let win = app.window_rect();
let num_bars = 20;
let bar_width = win.w() / num_bars as f32;

for i in 0..num_bars {
let x = map_range(i, 0, num_bars, win.left(), win.right());
let y = map_range(i, 0, num_bars, win.top(), win.bottom());

draw.rect()
.x_y(x, y)
.w_h(bar_width, win.h() / num_bars as f32)
.color(BLACK);
}
draw.tri()
.points(win.bottom_left(), win.top_left(), win.top_right())
.color(VIOLET);

// Draw an ellipse to follow the mouse().
let t = app.time().elapsed_seconds();
draw.ellipse()
.x_y(app.mouse().x * t.cos(), app.mouse().y)
.radius(win.w() * 0.125 * t.sin())
.color(RED);

// Draw a line!
draw.line()
.weight(10.0 + (t.sin() * 0.5 + 0.5) * 90.0)
.caps_round()
.color(PALE_GOLDENROD)
.points(win.top_left() * t.sin(), win.bottom_right() * t.cos());

// Draw a quad that follows the inverse of the ellipse.
draw.quad()
.x_y(-app.mouse().x, app.mouse().y)
.color(DARK_GREEN)
.rotate(t);

// Draw a rect that follows a different inverse of the ellipse.
draw.rect()
.x_y(app.mouse().y, app.mouse().x)
.w(app.mouse().x * 0.25)
.hsv(t, 1.0, 1.0);
}
2 changes: 1 addition & 1 deletion examples/draw/draw_texture_sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn model(app: &App) -> Model {
}

fn update(app: &App, model: &mut Model) {
let mut images = app.images_mut();
let mut images = app.assets_mut::<Image>();
let Some(image) = images.get_mut(&model.texture) else {
return;
};
Expand Down
2 changes: 1 addition & 1 deletion generative_design/image/p_4_1_1_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn crop_tiles(app: &App, model: &mut Model, win: geom::Rect) {
win.bottom() + (model.tile_height / 2.0),
);

let images = app.images();
let images = app.assets::<Image>();
let image = images.get(&model.texture).unwrap();
let image = image.clone().try_into_dynamic().unwrap();
let (w, h) = image.dimensions();
Expand Down
2 changes: 1 addition & 1 deletion generative_design/image/p_4_1_2_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn model(app: &App) -> Model {

// Draw the state of your `Model` into the given `Frame` here.
fn view(app: &App, model: &Model) {
let images = app.images();
let images = app.assets::<Image>();
let Some(texture) = images.get(&model.texture) else {
return;
};
Expand Down
2 changes: 1 addition & 1 deletion generative_design/image/p_4_1_2_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn model(app: &App) -> Model {
// Draw the state of your `Model` into the given `Frame` here.
fn view(app: &App, model: &Model) {
let draw = app.draw();
let images = app.images();
let images = app.assets::<Image>();
let Some(texture) = images.get(&model.texture) else {
return;
};
Expand Down
2 changes: 1 addition & 1 deletion generative_design/image/p_4_3_1_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn view(app: &App, model: &Model) {
let draw = app.draw();
let win = app.window_rect();

let images = app.images();
let images = app.assets::<Image>();
let image = images.get(&model.image).unwrap();
let image = image.clone().try_into_dynamic().unwrap();
let (w, h) = image.dimensions();
Expand Down
2 changes: 1 addition & 1 deletion generative_design/image/p_4_3_2_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn view(app: &App, model: &Model) {
let mut y = win.top();
let mut counter = 0;

let images = app.images();
let images = app.assets::<Image>();
let image = images.get(&model.image).unwrap();
let image = image.clone().try_into_dynamic().unwrap();
let (w, h) = image.dimensions();
Expand Down
2 changes: 1 addition & 1 deletion generative_design/random_and_noise/m_1_3_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn view(app: &App, model: &Model) {
.into();

let image = Image::from_dynamic(image, true, default());
let mut images = app.images_mut();
let mut images = app.assets_mut::<Image>();
let image = images.add(image);

let draw = app.draw();
Expand Down
Loading

0 comments on commit bbe949e

Please sign in to comment.