Skip to content

Commit 5674886

Browse files
authored
Add reformat to the CI (#21)
1 parent df091d5 commit 5674886

File tree

7 files changed

+113
-79
lines changed

7 files changed

+113
-79
lines changed

examples/animation.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::f64::consts::PI;
21
use instant::Instant;
32
#[cfg(not(target_arch = "wasm32"))]
43
use rayon::prelude::*;
4+
use std::f64::consts::PI;
55
use swbuf::GraphicsContext;
66
use winit::event::{Event, WindowEvent};
77
use winit::event_loop::{ControlFlow, EventLoop};
@@ -42,12 +42,12 @@ fn main() {
4242
(size.width, size.height)
4343
};
4444

45-
if (width, height) != old_size{
45+
if (width, height) != old_size {
4646
old_size = (width, height);
4747
frames = pre_render_frames(width as usize, height as usize);
4848
};
4949

50-
let buffer = &frames[((elapsed*60.0).round() as usize).clamp(0, 59)];
50+
let buffer = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
5151
graphics_context.set_buffer(buffer.as_slice(), width as u16, height as u16);
5252
}
5353
Event::MainEventsCleared => {
@@ -64,17 +64,20 @@ fn main() {
6464
});
6565
}
6666

67-
fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{
68-
let render = |frame_id|{
69-
let elapsed = ((frame_id as f64)/(60.0))*2.0*PI;
67+
fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>> {
68+
let render = |frame_id| {
69+
let elapsed = ((frame_id as f64) / (60.0)) * 2.0 * PI;
7070

7171
(0..(width * height))
7272
.map(|index| {
73-
let y = ((index / width) as f64)/(height as f64);
74-
let x = ((index % width) as f64)/(width as f64);
75-
let red = ((((y + elapsed).sin()*0.5+0.5)*255.0).round() as u32).clamp(0, 255);
76-
let green = ((((x + elapsed).sin()*0.5+0.5)*255.0).round() as u32).clamp(0, 255);
77-
let blue = ((((y - elapsed).cos()*0.5+0.5)*255.0).round() as u32).clamp(0, 255);
73+
let y = ((index / width) as f64) / (height as f64);
74+
let x = ((index % width) as f64) / (width as f64);
75+
let red =
76+
((((y + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
77+
let green =
78+
((((x + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
79+
let blue =
80+
((((y - elapsed).cos() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
7881

7982
blue | (green << 8) | (red << 16)
8083
})

examples/fruit.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ use winit::window::WindowBuilder;
77
fn main() {
88
//see fruit.jpg.license for the license of fruit.jpg
99
let fruit = image::load_from_memory(include_bytes!("fruit.jpg")).unwrap();
10-
let buffer = fruit.pixels().map(|(_x, _y, pixel)|{
11-
let red = pixel.0[0] as u32;
12-
let green = pixel.0[1] as u32;
13-
let blue = pixel.0[2] as u32;
14-
15-
blue | (green << 8) | (red << 16)
16-
}).collect::<Vec<_>>();
10+
let buffer = fruit
11+
.pixels()
12+
.map(|(_x, _y, pixel)| {
13+
let red = pixel.0[0] as u32;
14+
let green = pixel.0[1] as u32;
15+
let blue = pixel.0[2] as u32;
16+
17+
blue | (green << 8) | (red << 16)
18+
})
19+
.collect::<Vec<_>>();
1720

1821
let event_loop = EventLoop::new();
1922
let window = WindowBuilder::new()

src/cg.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use crate::SwBufError;
2-
use raw_window_handle::AppKitWindowHandle;
3-
use core_graphics::base::{kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault};
2+
use core_graphics::base::{
3+
kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault,
4+
};
45
use core_graphics::color_space::CGColorSpace;
56
use core_graphics::data_provider::CGDataProvider;
67
use core_graphics::image::CGImage;
8+
use raw_window_handle::AppKitWindowHandle;
79

10+
use cocoa::appkit::{NSView, NSViewHeightSizable, NSViewWidthSizable, NSWindow};
811
use cocoa::base::{id, nil};
9-
use cocoa::appkit::{NSView, NSViewWidthSizable, NSViewHeightSizable, NSWindow};
1012
use cocoa::quartzcore::{CALayer, ContentsGravity};
1113
use foreign_types::ForeignType;
1214

@@ -30,14 +32,13 @@ impl CGImpl {
3032

3133
view.addSubview_(subview); // retains subview (+1) = 2
3234
let _: () = msg_send![subview, release]; // releases subview (-1) = 1
33-
Ok(Self{layer})
35+
Ok(Self { layer })
3436
}
3537

3638
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
3739
let color_space = CGColorSpace::create_device_rgb();
38-
let data = std::slice::from_raw_parts(
39-
buffer.as_ptr() as *const u8,
40-
buffer.len() * 4).to_vec();
40+
let data =
41+
std::slice::from_raw_parts(buffer.as_ptr() as *const u8, buffer.len() * 4).to_vec();
4142
let data_provider = CGDataProvider::from_buffer(Arc::new(data));
4243
let image = CGImage::new(
4344
width as usize,

src/error.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::error::Error;
21
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
2+
use std::error::Error;
33
use thiserror::Error;
44

55
#[derive(Error, Debug)]
@@ -12,7 +12,7 @@ pub enum SwBufError {
1212
human_readable_window_platform_name: &'static str,
1313
human_readable_display_platform_name: &'static str,
1414
window_handle: RawWindowHandle,
15-
display_handle: RawDisplayHandle
15+
display_handle: RawDisplayHandle,
1616
},
1717

1818
#[error("The provided window handle is null.")]
@@ -22,13 +22,19 @@ pub enum SwBufError {
2222
IncompleteDisplayHandle,
2323

2424
#[error("Platform error")]
25-
PlatformError(Option<String>, Option<Box<dyn Error>>)
25+
PlatformError(Option<String>, Option<Box<dyn Error>>),
2626
}
2727

2828
#[allow(unused)] // This isn't used on all platforms
29-
pub(crate) fn unwrap<T, E: std::error::Error + 'static>(res: Result<T, E>, str: &str) -> Result<T, SwBufError>{
30-
match res{
29+
pub(crate) fn unwrap<T, E: std::error::Error + 'static>(
30+
res: Result<T, E>,
31+
str: &str,
32+
) -> Result<T, SwBufError> {
33+
match res {
3134
Ok(t) => Ok(t),
32-
Err(e) => Err(SwBufError::PlatformError(Some(str.into()), Some(Box::new(e))))
35+
Err(e) => Err(SwBufError::PlatformError(
36+
Some(str.into()),
37+
Some(Box::new(e)),
38+
)),
3339
}
3440
}

src/lib.rs

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,35 @@
55
extern crate objc;
66
extern crate core;
77

8-
#[cfg(target_os = "windows")]
9-
mod win32;
108
#[cfg(target_os = "macos")]
119
mod cg;
12-
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
13-
mod x11;
10+
#[cfg(target_os = "redox")]
11+
mod orbital;
1412
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
1513
mod wayland;
1614
#[cfg(target_arch = "wasm32")]
1715
mod web;
18-
#[cfg(target_os = "redox")]
19-
mod orbital;
16+
#[cfg(target_os = "windows")]
17+
mod win32;
18+
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
19+
mod x11;
2020

2121
mod error;
2222

2323
pub use error::SwBufError;
2424

25-
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle};
25+
use raw_window_handle::{
26+
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
27+
};
2628

2729
/// An instance of this struct contains the platform-specific data that must be managed in order to
2830
/// write to a window on that platform.
2931
pub struct GraphicsContext {
3032
/// The inner static dispatch object.
31-
///
33+
///
3234
/// This is boxed so that `GraphicsContext` is the same size on every platform, which should
3335
/// hopefully prevent surprises.
34-
graphics_context_impl: Box<Dispatch>
36+
graphics_context_impl: Box<Dispatch>,
3537
}
3638

3739
/// A macro for creating the enum used to statically dispatch to the platform-specific implementation.
@@ -84,7 +86,10 @@ impl GraphicsContext {
8486
///
8587
/// - Ensure that the provided objects are valid to draw a 2D buffer to, and are valid for the
8688
/// lifetime of the GraphicsContext
87-
pub unsafe fn new<W: HasRawWindowHandle, D: HasRawDisplayHandle>(window: &W, display: &D) -> Result<Self, SwBufError> {
89+
pub unsafe fn new<W: HasRawWindowHandle, D: HasRawDisplayHandle>(
90+
window: &W,
91+
display: &D,
92+
) -> Result<Self, SwBufError> {
8893
Self::from_raw(window.raw_window_handle(), display.raw_display_handle())
8994
}
9095

@@ -94,30 +99,54 @@ impl GraphicsContext {
9499
///
95100
/// - Ensure that the provided handles are valid to draw a 2D buffer to, and are valid for the
96101
/// lifetime of the GraphicsContext
97-
pub unsafe fn from_raw(raw_window_handle: RawWindowHandle, raw_display_handle: RawDisplayHandle) -> Result<Self, SwBufError> {
102+
pub unsafe fn from_raw(
103+
raw_window_handle: RawWindowHandle,
104+
raw_display_handle: RawDisplayHandle,
105+
) -> Result<Self, SwBufError> {
98106
let imple: Dispatch = match (raw_window_handle, raw_display_handle) {
99107
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
100-
(RawWindowHandle::Xlib(xlib_window_handle), RawDisplayHandle::Xlib(xlib_display_handle)) => Dispatch::X11(x11::X11Impl::new(xlib_window_handle, xlib_display_handle)?),
108+
(
109+
RawWindowHandle::Xlib(xlib_window_handle),
110+
RawDisplayHandle::Xlib(xlib_display_handle),
111+
) => Dispatch::X11(x11::X11Impl::new(xlib_window_handle, xlib_display_handle)?),
101112
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
102-
(RawWindowHandle::Wayland(wayland_window_handle), RawDisplayHandle::Wayland(wayland_display_handle)) => Dispatch::Wayland(wayland::WaylandImpl::new(wayland_window_handle, wayland_display_handle)?),
113+
(
114+
RawWindowHandle::Wayland(wayland_window_handle),
115+
RawDisplayHandle::Wayland(wayland_display_handle),
116+
) => Dispatch::Wayland(wayland::WaylandImpl::new(
117+
wayland_window_handle,
118+
wayland_display_handle,
119+
)?),
103120
#[cfg(target_os = "windows")]
104-
(RawWindowHandle::Win32(win32_handle), _) => Dispatch::Win32(win32::Win32Impl::new(&win32_handle)?),
121+
(RawWindowHandle::Win32(win32_handle), _) => {
122+
Dispatch::Win32(win32::Win32Impl::new(&win32_handle)?)
123+
}
105124
#[cfg(target_os = "macos")]
106-
(RawWindowHandle::AppKit(appkit_handle), _) => Dispatch::CG(cg::CGImpl::new(appkit_handle)?),
125+
(RawWindowHandle::AppKit(appkit_handle), _) => {
126+
Dispatch::CG(cg::CGImpl::new(appkit_handle)?)
127+
}
107128
#[cfg(target_arch = "wasm32")]
108129
(RawWindowHandle::Web(web_handle), _) => Dispatch::Web(web::WebImpl::new(web_handle)?),
109130
#[cfg(target_os = "redox")]
110-
(RawWindowHandle::Orbital(orbital_handle), _) => Dispatch::Orbital(orbital::OrbitalImpl::new(orbital_handle)?),
111-
(unimplemented_window_handle, unimplemented_display_handle) => return Err(SwBufError::UnsupportedPlatform {
112-
human_readable_window_platform_name: window_handle_type_name(&unimplemented_window_handle),
113-
human_readable_display_platform_name: display_handle_type_name(&unimplemented_display_handle),
114-
window_handle: unimplemented_window_handle,
115-
display_handle: unimplemented_display_handle
116-
}),
131+
(RawWindowHandle::Orbital(orbital_handle), _) => {
132+
Dispatch::Orbital(orbital::OrbitalImpl::new(orbital_handle)?)
133+
}
134+
(unimplemented_window_handle, unimplemented_display_handle) => {
135+
return Err(SwBufError::UnsupportedPlatform {
136+
human_readable_window_platform_name: window_handle_type_name(
137+
&unimplemented_window_handle,
138+
),
139+
human_readable_display_platform_name: display_handle_type_name(
140+
&unimplemented_display_handle,
141+
),
142+
window_handle: unimplemented_window_handle,
143+
display_handle: unimplemented_display_handle,
144+
})
145+
}
117146
};
118147

119148
Ok(Self {
120-
graphics_context_impl: Box::new(imple)
149+
graphics_context_impl: Box::new(imple),
121150
})
122151
}
123152

src/orbital.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use raw_window_handle::OrbitalWindowHandle;
2-
use std::{
3-
cmp,
4-
slice,
5-
str,
6-
};
2+
use std::{cmp, slice, str};
73

84
use crate::SwBufError;
95

@@ -19,12 +15,15 @@ impl OrbitalMap {
1915
let size = pages * syscall::PAGE_SIZE;
2016

2117
// Map window buffer
22-
let address = syscall::fmap(fd, &syscall::Map {
23-
offset: 0,
24-
size,
25-
flags: syscall::PROT_READ | syscall::PROT_WRITE,
26-
address: 0,
27-
})?;
18+
let address = syscall::fmap(
19+
fd,
20+
&syscall::Map {
21+
offset: 0,
22+
size,
23+
flags: syscall::PROT_READ | syscall::PROT_WRITE,
24+
address: 0,
25+
},
26+
)?;
2827

2928
Ok(Self { address, size })
3029
}
@@ -34,8 +33,7 @@ impl Drop for OrbitalMap {
3433
fn drop(&mut self) {
3534
unsafe {
3635
// Unmap window buffer on drop
37-
syscall::funmap(self.address, self.size)
38-
.expect("failed to unmap orbital window");
36+
syscall::funmap(self.address, self.size).expect("failed to unmap orbital window");
3937
}
4038
}
4139
}
@@ -71,15 +69,13 @@ impl OrbitalImpl {
7169

7270
{
7371
// Map window buffer
74-
let window_map = OrbitalMap::new(
75-
window_fd,
76-
window_width * window_height * 4
77-
).expect("failed to map orbital window");
72+
let window_map = OrbitalMap::new(window_fd, window_width * window_height * 4)
73+
.expect("failed to map orbital window");
7874

7975
// Window buffer is u32 color data in 0xAABBGGRR format
8076
let window_data = slice::from_raw_parts_mut(
8177
window_map.address as *mut u32,
82-
window_width * window_height
78+
window_width * window_height,
8379
);
8480

8581
// Copy each line, cropping to fit
@@ -90,9 +86,8 @@ impl OrbitalImpl {
9086
for y in 0..min_height {
9187
let offset_buffer = y * width;
9288
let offset_data = y * window_width;
93-
window_data[offset_data..offset_data + min_width].copy_from_slice(
94-
&buffer[offset_buffer..offset_buffer + min_width]
95-
);
89+
window_data[offset_data..offset_data + min_width]
90+
.copy_from_slice(&buffer[offset_buffer..offset_buffer + min_width]);
9691
}
9792

9893
// Window buffer map is dropped here

src/web.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ impl WebImpl {
3232
// `querySelector` only throws an error if the selector is invalid.
3333
.unwrap()
3434
.ok_or_else(|| {
35-
SwBufError::PlatformError(
36-
Some("No canvas found with the given id".into()),
37-
None,
38-
)
35+
SwBufError::PlatformError(Some("No canvas found with the given id".into()), None)
3936
})?
4037
// We already made sure this was a canvas in `querySelector`.
4138
.unchecked_into();

0 commit comments

Comments
 (0)