Skip to content

Commit b9d6362

Browse files
committed
update to neon 0.10 – buffer access
- no longer has to happen in a borrow closure
1 parent 33a7c49 commit b9d6362

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

src/canvas.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(non_snake_case)]
22
use std::cell::RefCell;
3-
use neon::prelude::*;
3+
use neon::{prelude::*, types::buffer::TypedArray};
44

55
use crate::utils::*;
66
use crate::context::page::pages_arg;
@@ -94,10 +94,8 @@ pub fn toBuffer(mut cx: FunctionContext) -> JsResult<JsUndefined> {
9494

9595
let args = match encoded{
9696
Ok(data) => {
97-
let mut buffer = JsBuffer::new(&mut cx, data.len() as u32).unwrap();
98-
cx.borrow_mut(&mut buffer, |buf_data| {
99-
buf_data.as_mut_slice().copy_from_slice(&data);
100-
});
97+
let mut buffer = cx.buffer(data.len())?;
98+
buffer.as_mut_slice(&mut cx).copy_from_slice(&data);
10199
vec![
102100
cx.string("ok").upcast::<JsValue>(),
103101
buffer.upcast::<JsValue>(),
@@ -136,10 +134,8 @@ pub fn toBufferSync(mut cx: FunctionContext) -> JsResult<JsValue> {
136134

137135
match encoded{
138136
Ok(data) => {
139-
let mut buffer = JsBuffer::new(&mut cx, data.len() as u32).unwrap();
140-
cx.borrow_mut(&mut buffer, |buf_data| {
141-
buf_data.as_mut_slice().copy_from_slice(&data);
142-
});
137+
let mut buffer = cx.buffer(data.len())?;
138+
buffer.as_mut_slice(&mut cx).copy_from_slice(&data);
143139
Ok(buffer.upcast::<JsValue>())
144140
},
145141
Err(msg) => cx.throw_error(msg)

src/context/api.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(non_snake_case)]
66
use std::f32::consts::PI;
77
use std::cell::RefCell;
8-
use neon::prelude::*;
8+
use neon::{prelude::*, types::buffer::TypedArray};
99
use skia_safe::{Point, Rect, Matrix, Path, PathDirection, PaintStyle};
1010
use skia_safe::path::AddPathMode::Append;
1111
use skia_safe::path::AddPathMode::Extend;
@@ -739,10 +739,9 @@ pub fn getImageData(mut cx: FunctionContext) -> JsResult<JsBuffer> {
739739
let width = float_arg(&mut cx, 3, "width")? as i32;
740740
let height = float_arg(&mut cx, 4, "height")? as i32;
741741

742-
let buffer = JsBuffer::new(&mut cx, 4 * (width * height) as u32)?;
743-
cx.borrow(&buffer, |data| {
744-
this.get_pixels(data.as_mut_slice(), (x, y), (width, height));
745-
});
742+
let mut buffer = cx.buffer(4 * (width * height) as usize)?;
743+
this.get_pixels(buffer.as_mut_slice(&mut cx), (x, y), (width, height));
744+
746745
Ok(buffer)
747746
}
748747

@@ -771,11 +770,9 @@ pub fn putImageData(mut cx: FunctionContext) -> JsResult<JsUndefined> {
771770
Rect::from_xywh(x, y, width, height)
772771
)};
773772

774-
let buffer = img_data.get(&mut cx, "data")?.downcast_or_throw::<JsBuffer, _>(&mut cx)?;
773+
let buffer: Handle<JsBuffer> = img_data.get(&mut cx, "data")?;
775774
let info = Image::info(width, height);
776-
cx.borrow(&buffer, |data| {
777-
this.blit_pixels(data.as_slice(), &info, &src, &dst);
778-
});
775+
this.blit_pixels(buffer.as_slice(&cx), &info, &src, &dst);
779776
Ok(cx.undefined())
780777
}
781778

src/image.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(unused_variables)]
44
#![allow(dead_code)]
55
use std::cell::RefCell;
6-
use neon::prelude::*;
6+
use neon::{prelude::*, types::buffer::TypedArray};
77
use skia_safe::{Image as SkImage, ImageInfo, Size, ColorType, AlphaType, Data};
88

99
use crate::utils::*;
@@ -64,9 +64,7 @@ pub fn set_data(mut cx: FunctionContext) -> JsResult<JsBoolean> {
6464
let mut this = this.borrow_mut();
6565

6666
let buffer = cx.argument::<JsBuffer>(1)?;
67-
let data = cx.borrow(&buffer, |buf_data| {
68-
Data::new_copy(buf_data.as_slice())
69-
});
67+
let data = Data::new_copy(buffer.as_slice(&mut cx));
7068

7169
this.image = SkImage::from_encoded(data);
7270
Ok(cx.boolean(this.image.is_some()))

0 commit comments

Comments
 (0)