Skip to content

Commit

Permalink
refactor: Make IsJanetAbstract trait an unsafe trait
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This changes how the trait is implemented, requiring the implementer to use the unsafe keyword
  • Loading branch information
GrayJack committed Nov 27, 2024
1 parent 4b9ead7 commit a31c536
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to the library should be put here
- **Breaking** Refactor: Turn `JanetConversionError` into a enum
- **Breaking** Refactor: Refactor `CFunOptions` to use `CStr` instead of `str`
- **Breaking** Refactor: Use `usize` for length/index/capacity in collections
- **Breaking** Refactor: Make `IsJanetAbstract` an unsafe trait
- **Breaking** Feat: Make `amalgation` feature enabled by default
- Feat: Add `Janet::dynamic_from_cstr` constructor
- Feat: Add `JanetArgs::get_value` and `JanetArgs::get_tagged` trait methods
Expand Down
23 changes: 16 additions & 7 deletions src/types/abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,17 @@ impl Ord for JanetAbstract {
}

/// The trait that encodes the information required to instantiate the implementer as
/// [`JanetAbstract`]
pub trait IsJanetAbstract {
/// [`JanetAbstract`].
///
/// # Safety
/// Implementing this trait is not trivial if the type implements [`Drop`] and can cause
/// undefined behavior if not implemented carefully.
///
/// Usually that can be solved by defining [`Get`](IsJanetAbstract::Get) as a
/// [`ManuallyDrop<Self>`]. But in that case, you also become responsible to define
/// [`JanetAbstractType::gc`] function to properly handle the dropping of the type for the
/// Janet-side.
pub unsafe trait IsJanetAbstract {
/// The type that you get when you call [`JanetAbstract::get`] family of functions.
///
/// This is usually set to `Self` when the type does not implement [`Drop`], or
Expand Down Expand Up @@ -375,7 +384,7 @@ pub fn register<T: IsJanetAbstract>() {
}
}

impl IsJanetAbstract for i64 {
unsafe impl IsJanetAbstract for i64 {
type Get = i64;

const SIZE: usize = core::mem::size_of::<Self>();
Expand All @@ -386,7 +395,7 @@ impl IsJanetAbstract for i64 {
}
}

impl IsJanetAbstract for u64 {
unsafe impl IsJanetAbstract for u64 {
type Get = u64;

const SIZE: usize = core::mem::size_of::<Self>();
Expand All @@ -397,7 +406,7 @@ impl IsJanetAbstract for u64 {
}
}

impl<A> IsJanetAbstract for ManuallyDrop<A>
unsafe impl<A> IsJanetAbstract for ManuallyDrop<A>
where
A: IsJanetAbstract,
{
Expand Down Expand Up @@ -490,7 +499,7 @@ mod tests {
bytes: None,
};

impl IsJanetAbstract for TestDrop {
unsafe impl IsJanetAbstract for TestDrop {
type Get = ManuallyDrop<Self>;

const SIZE: usize = core::mem::size_of::<Self>();
Expand Down Expand Up @@ -538,7 +547,7 @@ mod tests {
bytes: None,
};

impl IsJanetAbstract for TestDrop2 {
unsafe impl IsJanetAbstract for TestDrop2 {
type Get = Self;

const SIZE: usize = core::mem::size_of::<Self>();
Expand Down
2 changes: 1 addition & 1 deletion src/types/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl JanetFile {
}
}

impl IsJanetAbstract for JanetFile {
unsafe impl IsJanetAbstract for JanetFile {
type Get = Self;

const SIZE: usize = mem::size_of::<evil_janet::JanetFile>();
Expand Down
2 changes: 1 addition & 1 deletion src/types/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl fmt::Debug for JanetRng {
}
}

impl IsJanetAbstract for JanetRng {
unsafe impl IsJanetAbstract for JanetRng {
type Get = Self;

const SIZE: usize = mem::size_of::<Self>();
Expand Down

0 comments on commit a31c536

Please sign in to comment.