Skip to content

Commit

Permalink
Attachment loader cleanup (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuwu authored Nov 15, 2023
1 parent 760f1f7 commit c9df1c9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 0.7.0 (UNRELEASED)
- Add `AttachmentLoader` for creating region attachments
- Upstream fixes
- Reduce allocations ([EsotericSoftware/spine-runtimes#2325](https://github.com/EsotericSoftware/spine-runtimes/issues/2325))
- Fix double free of sequences in mesh attachments ([EsotericSoftware/spine-runtimes#2394](https://github.com/EsotericSoftware/spine-runtimes/issues/2394))
Expand Down
1 change: 1 addition & 0 deletions ci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fn doc_check(sh: &Shell) -> anyhow::Result<()> {
sh,
"cargo doc --workspace --all-features --no-deps --document-private-items"
)
.env("RUSTDOCFLAGS", "-Dwarnings")
.run()?;
Ok(())
}
Expand Down
44 changes: 14 additions & 30 deletions src/attachment_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ use crate::{
spAttachmentLoader_dispose,
},
c_interface::{NewFromPtr, SyncPtr},
Atlas, Attachment, AttachmentType, RegionProps, Skin,
Atlas, Attachment, AttachmentType, RegionProps, Skin, SpineError,
};

/// Error types related to [`AttachmentLoader`](`crate::AttachmentLoader`).
#[derive(Debug)]
pub enum AttachmentLoaderError {
/// Creating an attachment failed.
/// Check [`error1`](`Self::error1`) and [`error2`](`Self::error2`) for more information.
CreateAttachmentFailed,
InvalidArgument {
field: &'static str,
},
}

/// A loader for creating custom attachments.
///
/// Currently only supports [`Atlas`](`crate::Atlas`) based attachments.
#[derive(Debug)]
pub struct AttachmentLoader {
c_attachment_loader: SyncPtr<spAttachmentLoader>,
Expand Down Expand Up @@ -46,20 +38,18 @@ impl AttachmentLoader {
///
/// # Errors
///
/// Returns [`AttachmentLoaderError::CreateAttachmentFailed`] if creating the attachment failed.
/// Returns [`SpineError::CreationFailed`] if creating the attachment failed.
/// Check [`error1`](`Self::error1`) and [`error2`](`Self::error2`) for more information.
/// Returns [`AttachmentLoaderError::InvalidArgument`] if `name` or `path` contain a null byte.
/// Returns [`SpineError::NulError`] if `name` or `path` contain a null byte.
pub fn create_attachment(
&self,
skin: Option<Skin>,
attachment_type: AttachmentType,
name: &str,
path: &str,
) -> Result<Attachment, AttachmentLoaderError> {
let c_name = std::ffi::CString::new(name)
.map_err(|_| AttachmentLoaderError::InvalidArgument { field: "name" })?;
let c_path = std::ffi::CString::new(path)
.map_err(|_| AttachmentLoaderError::InvalidArgument { field: "path" })?;
) -> Result<Attachment, SpineError> {
let c_name = std::ffi::CString::new(name)?;
let c_path = std::ffi::CString::new(path)?;

unsafe {
let c_name = c_name.as_ptr();
Expand All @@ -77,7 +67,7 @@ impl AttachmentLoader {
);

if attachment.is_null() {
Err(AttachmentLoaderError::CreateAttachmentFailed)
Err(SpineError::new_creation_failed("Attachment"))
} else {
Ok(Attachment::new_from_ptr(attachment))
}
Expand All @@ -88,20 +78,20 @@ impl AttachmentLoader {
///
/// # Errors
///
/// Returns [`AttachmentLoaderError::CreateAttachmentFailed`] if creating the attachment failed.
/// Returns [`SpineError::CreationFailed`] if creating the attachment failed.
/// Check [`error1`](`Self::error1`) and [`error2`](`Self::error2`) for more information.
/// Returns [`AttachmentLoaderError::InvalidArgument`] if `name` or `path` contain a null byte.
/// Returns [`SpineError::NulError`] if `name` or `path` contain a null byte.
pub fn create_region_attachment(
&self,
skin: Option<Skin>,
name: &str,
path: &str,
props: &RegionProps,
) -> Result<Attachment, AttachmentLoaderError> {
) -> Result<Attachment, SpineError> {
let attachment = self.create_attachment(skin, AttachmentType::Region, name, path)?;

let Some(mut region) = attachment.as_region() else {
return Err(AttachmentLoaderError::CreateAttachmentFailed);
return Err(SpineError::new_creation_failed("RegionAttachment"));
};

region.update_from_props(props);
Expand All @@ -114,12 +104,6 @@ impl AttachmentLoader {
c_ptr!(c_attachment_loader, spAttachmentLoader);
}

impl Clone for AttachmentLoader {
fn clone(&self) -> Self {
unsafe { AttachmentLoader::new_from_ptr(self.c_ptr()) }
}
}

impl Drop for AttachmentLoader {
fn drop(&mut self) {
unsafe {
Expand Down
12 changes: 12 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum SpineError {
FailedToReadFile { file: String },
/// An error when a specified path is not utf-8.
PathNotUtf8,
/// Failed to create the requested type.
CreationFailed { what: String },
}

impl SpineError {
Expand All @@ -28,6 +30,12 @@ impl SpineError {
name: name.to_owned(),
}
}

pub(crate) fn new_creation_failed(what: &str) -> Self {
Self::CreationFailed {
what: what.to_owned(),
}
}
}

impl From<NulError> for SpineError {
Expand Down Expand Up @@ -60,6 +68,10 @@ impl fmt::Display for SpineError {
write!(f, "Path not utf-8")?;
Ok(())
}
SpineError::CreationFailed { what } => {
write!(f, "Failed to create {what}")?;
Ok(())
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/region_attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
#[cfg(feature = "mint")]
use mint::Vector2;

/// Properties for updating [`RegionAttachment`].
#[derive(Debug)]
pub struct RegionProps {
pub x: f32,
Expand Down

0 comments on commit c9df1c9

Please sign in to comment.