-
Notifications
You must be signed in to change notification settings - Fork 0
Fix ref type #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix ref type #62
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e11c06d
removed unnecessary mut
hazelwiss 00c5586
allows testing references in `bauble_test`. Allows converting from a …
hazelwiss 72a4826
added test cases for bauble references
hazelwiss 79d3375
Merge branch 'main' into fix_ref_type
hazelwiss 110d94d
added ref builtin
hazelwiss 9c8986d
added checking explicitly typed reference correctness
hazelwiss 4ad1b6a
addressed part of review
hazelwiss 30872b7
addressed part of review
hazelwiss 7cac810
fixed up doc comment
hazelwiss b087c04
fixed incorrectly pasted tests
hazelwiss eb38e53
fixed issue with checking for incorrect type when resolving references
hazelwiss 3aad602
ignoring codeblock for tests
hazelwiss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| use crate::{ | ||
| Bauble, BaubleAllocator, ToRustErrorKind, Val, Value, | ||
| path::TypePath, | ||
| types::{Type, TypeRegistry}, | ||
| }; | ||
| use std::{cell::UnsafeCell, fmt::Debug, marker::PhantomData}; | ||
|
|
||
| /// The builtin reference type. | ||
| /// | ||
| /// This corresponds to the type used internally by Bauble for objects which are | ||
| /// references. That is to say, if you were to write | ||
| /// ```ignore | ||
| /// own: MyType = MyType { ... } | ||
| /// ref = $own | ||
| /// ``` | ||
| /// then `ref` has the type `Ref<MyType>` and the value `own`. | ||
| /// | ||
| /// This type is not required for parsing or using references. Custom types like | ||
| /// `MyRefType` can still be made to be used to parse various references and be | ||
| /// used in Bauble. This is a convenience type and is capable of handling various | ||
| /// references without needing to be registered to Bauble manually. Besides | ||
| /// convenience this type offers no advantage, and if special behaviour is requred | ||
| /// for parsing and handling references, prefer to use a custom type from Rust. | ||
| /// | ||
| /// `S` is the inner representation used for `TypePath`. | ||
| pub struct Ref<T, S = String> { | ||
| /// The path to the referenced asset. | ||
| pub path: TypePath<S>, | ||
| /// Invariant over `T`. | ||
| _mark: PhantomData<UnsafeCell<T>>, | ||
| } | ||
|
|
||
| impl<T, S> Ref<T, S> { | ||
| /// Create a reference from the specified path. The path must not be valid. | ||
| pub fn from_path(path: TypePath<S>) -> Self { | ||
| Self { | ||
| path, | ||
| _mark: PhantomData, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T, S: PartialEq> PartialEq for Ref<T, S> { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.path == other.path | ||
| } | ||
| } | ||
| impl<T, S: Eq> Eq for Ref<T, S> {} | ||
|
|
||
| impl<T, S: Debug> Debug for Ref<T, S> { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| Debug::fmt(&self.path, f) | ||
| } | ||
| } | ||
|
|
||
| impl<'b, A: BaubleAllocator<'b>, T: Bauble<'b, A>> Bauble<'b, A> for Ref<T, A::TypePathInner> { | ||
| fn builtin(registry: &mut TypeRegistry) -> Option<crate::types::TypeId> { | ||
| let inner = registry.get_or_register_type::<T, A>(); | ||
| Some(registry.get_or_register_asset_ref(inner)) | ||
| } | ||
|
|
||
| fn construct_type(_registry: &mut TypeRegistry) -> Type { | ||
| unreachable!("This is a builtin and should never be constructed."); | ||
| } | ||
|
|
||
| fn from_bauble( | ||
| val: Val, | ||
| allocator: &A, | ||
| ) -> Result<<A as BaubleAllocator<'b>>::Out<Self>, crate::ToRustError> { | ||
| match val.value.value { | ||
| Value::Ref(r) => Ok({ | ||
| let path = allocator.wrap_type_path(r); | ||
| let value = Self { | ||
| // SAFETY: path was derived from `allocator`. | ||
| path: unsafe { allocator.validate(path)? }, | ||
| _mark: PhantomData, | ||
| }; | ||
|
|
||
| // SAFETY: allocator has wrapped the type path. | ||
| unsafe { allocator.wrap(value) } | ||
| }), | ||
| _ => Err(Self::error( | ||
| val.value.span, | ||
| ToRustErrorKind::WrongType { found: val.ty }, | ||
| )), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -243,19 +243,26 @@ pub trait BaubleAllocator<'a> { | |
| where | ||
| Self: 'a; | ||
|
|
||
| /// The inner representation of a type path created by this allocator. | ||
| type TypePathInner: 'static; | ||
|
|
||
| /// # Safety | ||
| /// Allocations in `value` have to be allocated with this allocator | ||
| unsafe fn wrap<T>(&self, value: T) -> Self::Out<T>; | ||
| /// # Safety | ||
| /// If validated an item must be placed within the same allocator. | ||
| unsafe fn validate<T>(&self, value: Self::Out<T>) -> Result<T, ToRustError>; | ||
|
|
||
| /// Create a type path from this allocator. | ||
| fn wrap_type_path(&self, path: TypePath) -> Self::Out<TypePath<Self::TypePathInner>>; | ||
| } | ||
|
|
||
| /// The standard Rust allocator when used by Bauble. | ||
| pub struct DefaultAllocator; | ||
|
|
||
| impl BaubleAllocator<'_> for DefaultAllocator { | ||
| type Out<T> = T; | ||
| type TypePathInner = String; | ||
|
|
||
| unsafe fn wrap<T>(&self, value: T) -> Self::Out<T> { | ||
| value | ||
|
|
@@ -264,10 +271,22 @@ impl BaubleAllocator<'_> for DefaultAllocator { | |
| unsafe fn validate<T>(&self, value: Self::Out<T>) -> Result<T, ToRustError> { | ||
| Ok(value) | ||
| } | ||
|
|
||
| fn wrap_type_path(&self, path: TypePath) -> TypePath<Self::TypePathInner> { | ||
| path | ||
| } | ||
| } | ||
|
|
||
| /// The trait used by types usable by Bauble. Any type that can be parsed by bauble should implement this trait. | ||
| pub trait Bauble<'a, A: BaubleAllocator<'a> = DefaultAllocator>: Sized + 'static { | ||
| /// # DON'T CALL THIS, call `TypeRegistry::get_or_register_type` instead. | ||
| /// | ||
| /// Constructs a builtin type. A builtin type is a type which is not constructed by Bauble during context | ||
| /// construction and might warrant additional rules. | ||
| fn builtin(#[expect(unused)] registry: &mut types::TypeRegistry) -> Option<TypeId> { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed to add builtin types to avoid |
||
| None | ||
| } | ||
|
|
||
| /// # DON'T CALL THIS, call `TypeRegistry::get_or_register_type` instead. | ||
| /// | ||
| /// Constructs a reflection type that bauble uses to parse and resolve types. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.