From dab7cb6bbd544e83863fc13ae05e7e471de22fe6 Mon Sep 17 00:00:00 2001 From: Alejandro Perea <alexpro820@gmail.com> Date: Thu, 3 Mar 2022 15:34:58 +0100 Subject: [PATCH] Add some missing attributes (#173) * Add attributes - `#[inline]` for properties - `#![deny(unsafe_code)]` - `#![deny(missing_copy_implementations)]` - `#![deny(missing_debug_implementations)]` * Add more inline attributes * Merge with upstream --- src/animation.rs | 2 +- src/cache.rs | 1 + src/layers/group.rs | 1 + src/layers/image.rs | 1 + src/layers/mod.rs | 12 ++++++++++++ src/layers/object.rs | 2 ++ src/layers/tile/finite.rs | 2 ++ src/layers/tile/mod.rs | 6 ++++++ src/lib.rs | 4 ++++ src/map.rs | 1 + src/objects.rs | 11 +++++++++++ src/tile.rs | 1 + src/tileset.rs | 1 + src/util.rs | 4 +++- 14 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/animation.rs b/src/animation.rs index bf0d536..691b531 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -2,7 +2,7 @@ use xml::attribute::OwnedAttribute; use crate::{error::TiledError, util::{get_attrs, XmlEventResult, parse_tag}}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Copy)] pub struct Frame { pub tile_id: u32, pub duration: u32, diff --git a/src/cache.rs b/src/cache.rs index 4d670a1..a193a1a 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -21,6 +21,7 @@ pub trait ResourceCache { } /// A cache that identifies resources by their path in the user's filesystem. +#[derive(Debug)] pub struct FilesystemResourceCache { tilesets: HashMap<ResourcePathBuf, Arc<Tileset>>, } diff --git a/src/layers/group.rs b/src/layers/group.rs index 4e03fe6..f4d135d 100644 --- a/src/layers/group.rs +++ b/src/layers/group.rs @@ -93,6 +93,7 @@ impl<'map> GroupLayer<'map> { } /// An iterator that iterates over all the layers in a group layer, obtained via [`GroupLayer::layers`]. +#[derive(Debug)] pub struct GroupLayerIter<'map> { map: &'map Map, group: &'map GroupLayerData, diff --git a/src/layers/image.rs b/src/layers/image.rs index de2d978..e75a92b 100644 --- a/src/layers/image.rs +++ b/src/layers/image.rs @@ -39,6 +39,7 @@ map_wrapper!(ImageLayer => ImageLayerData); impl<'map> ImageLayer<'map> { /// Get a reference to the image layer's image. + #[inline] pub fn image(&self) -> Option<&Image> { self.data.image.as_ref() } diff --git a/src/layers/mod.rs b/src/layers/mod.rs index ed0de12..b0b0069 100644 --- a/src/layers/mod.rs +++ b/src/layers/mod.rs @@ -114,61 +114,73 @@ map_wrapper!(Layer => LayerData); impl<'map> Layer<'map> { /// Get a reference to the layer's name. + #[inline] pub fn name(&self) -> &str { self.data.name.as_ref() } /// Get the layer's id. + #[inline] pub fn id(&self) -> u32 { self.data.id } /// Whether this layer should be visible or not. + #[inline] pub fn visible(&self) -> bool { self.data.visible } /// Get the layer's x offset (in pixels). + #[inline] pub fn offset_x(&self) -> f32 { self.data.offset_x } /// Get the layer's y offset (in pixels). + #[inline] pub fn offset_y(&self) -> f32 { self.data.offset_y } /// Get the layer's x parallax factor. + #[inline] pub fn parallax_x(&self) -> f32 { self.data.parallax_x } /// Get the layer's y parallax factor. + #[inline] pub fn parallax_y(&self) -> f32 { self.data.parallax_y } /// Get the layer's opacity. + #[inline] pub fn opacity(&self) -> f32 { self.data.opacity } /// Get the layer's tint color. + #[inline] pub fn tint_color(&self) -> Option<Color> { self.data.tint_color } /// Get a reference to the layer's properties. + #[inline] pub fn properties(&self) -> &Properties { &self.data.properties } /// Get the layer's type. + #[inline] pub fn layer_type(&self) -> LayerType<'map> { LayerType::new(self.map, &self.data.layer_type) } } +#[derive(Debug)] pub enum LayerType<'map> { TileLayer(TileLayer<'map>), ObjectLayer(ObjectLayer<'map>), diff --git a/src/layers/object.rs b/src/layers/object.rs index efe2aa0..c726595 100644 --- a/src/layers/object.rs +++ b/src/layers/object.rs @@ -64,6 +64,7 @@ impl<'map> ObjectLayer<'map> { } /// An iterator that iterates over all the objects in an object layer, obtained via [`ObjectLayer::objects`]. +#[derive(Debug)] pub struct Objects<'map> { map: &'map Map, data: &'map ObjectLayerData, @@ -91,6 +92,7 @@ impl<'map> Iterator for Objects<'map> { } impl<'map> ExactSizeIterator for Objects<'map> { + #[inline] fn len(&self) -> usize { self.data.objects.len() - self.index } diff --git a/src/layers/tile/finite.rs b/src/layers/tile/finite.rs index 6ad46e8..2e2c0cc 100644 --- a/src/layers/tile/finite.rs +++ b/src/layers/tile/finite.rs @@ -70,11 +70,13 @@ impl<'map> FiniteTileLayer<'map> { } /// Get the tile layer's width in tiles. + #[inline] pub fn width(&self) -> u32 { self.data.width } /// Get the tile layer's height in tiles. + #[inline] pub fn height(&self) -> u32 { self.data.height } diff --git a/src/layers/tile/mod.rs b/src/layers/tile/mod.rs index 6ff195b..5c3d27a 100644 --- a/src/layers/tile/mod.rs +++ b/src/layers/tile/mod.rs @@ -125,31 +125,37 @@ impl<'map> LayerTile<'map> { /// /// Use [`LayerTile::get_tile`] if you want to obtain the [`Tile`] that this layer tile is /// referencing. + #[inline] pub fn tileset_index(&self) -> usize { self.data.tileset_index } /// Get the layer tile's local id within its parent tileset. + #[inline] pub fn id(&self) -> u32 { self.data.id } /// Whether this tile is flipped on its Y axis (horizontally). + #[inline] pub fn flip_h(&self) -> bool { self.data.flip_h } /// Whether this tile is flipped on its X axis (vertically). + #[inline] pub fn flip_v(&self) -> bool { self.data.flip_v } /// Whether this tile is flipped diagonally. + #[inline] pub fn flip_d(&self) -> bool { self.data.flip_d } } +#[derive(Debug)] pub enum TileLayer<'map> { Finite(FiniteTileLayer<'map>), Infinite(InfiniteTileLayer<'map>), diff --git a/src/lib.rs b/src/lib.rs index 45a03f3..0ec71af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,7 @@ +#![deny(unsafe_code)] +#![deny(missing_copy_implementations)] +#![deny(missing_debug_implementations)] + mod animation; mod cache; mod error; diff --git a/src/map.rs b/src/map.rs index 6eccabe..dcedbb2 100644 --- a/src/map.rs +++ b/src/map.rs @@ -116,6 +116,7 @@ impl Map { } /// An iterator that iterates over all the layers in a map, obtained via [`Map::layers`]. +#[derive(Debug)] pub struct MapLayerIter<'map> { map: &'map Map, index: usize, diff --git a/src/objects.rs b/src/objects.rs index 646577a..062270a 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -172,6 +172,7 @@ map_wrapper!(Object => ObjectData); impl<'map> Object<'map> { /// Get the object's id. + #[inline] pub fn id(&self) -> u32 { self.data.id } @@ -185,51 +186,61 @@ impl<'map> Object<'map> { } /// Get a reference to the object's name. + #[inline] pub fn name(&self) -> &str { self.data.name.as_ref() } /// Get a reference to the object's type. + #[inline] pub fn obj_type(&self) -> &str { self.data.obj_type.as_ref() } /// Get the object's width. + #[inline] pub fn width(&self) -> f32 { self.data.width } /// Get the object's height. + #[inline] pub fn height(&self) -> f32 { self.data.height } /// Get the object's x. + #[inline] pub fn x(&self) -> f32 { self.data.x } /// Get object's y. + #[inline] pub fn y(&self) -> f32 { self.data.y } /// Get a reference to the object's rotation. + #[inline] pub fn rotation(&self) -> f32 { self.data.rotation } /// Whether the object should be visible or not. + #[inline] pub fn visible(&self) -> bool { self.data.visible } /// Get a reference to the object's shape. + #[inline] pub fn shape(&self) -> &ObjectShape { &self.data.shape } /// Get a reference to the object's properties. + #[inline] pub fn properties(&self) -> &Properties { &self.data.properties } diff --git a/src/tile.rs b/src/tile.rs index c99301b..4ef80db 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -24,6 +24,7 @@ pub(crate) struct TileData { probability: f32, } +#[derive(Debug)] pub struct Tile<'tileset> { pub(crate) tileset: &'tileset Tileset, pub(crate) data: &'tileset TileData, diff --git a/src/tileset.rs b/src/tileset.rs index 6e81988..8745a80 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -106,6 +106,7 @@ impl Tileset { } /// Gets the tile with the specified ID from the tileset. + #[inline] pub fn get_tile(&self, id: u32) -> Option<Tile> { self.tiles.get(&id).map(|data| Tile::new(self, data)) } diff --git a/src/util.rs b/src/util.rs index 1afe902..dadad0c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -55,18 +55,20 @@ macro_rules! parse_tag { /// Creates a new type that wraps an internal data type over along with a map. macro_rules! map_wrapper { ($name:ident => $data_ty:ty) => { - #[derive(Clone, PartialEq, Debug)] + #[derive(Clone, Copy, PartialEq, Debug)] pub struct $name<'map> { pub(crate) map: &'map $crate::Map, pub(crate) data: &'map $data_ty, } impl<'map> $name<'map> { + #[inline] pub(crate) fn new(map: &'map $crate::Map, data: &'map $data_ty) -> Self { Self { map, data } } /// Get the map this object is from. + #[inline] pub fn map(&self) -> &'map $crate::Map { self.map } -- GitLab