Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "field_path"
description = "Type-safe, no-std field access and reflection utilities."
version = "0.2.1"
version = "0.3.0"
edition = "2024"
license = "MIT OR Apache-2.0"
repository = "https://github.com/voxell-tech/field_path"
Expand Down
8 changes: 5 additions & 3 deletions src/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<S, T> Accessor<S, T> {
(self.mut_fn)(source)
}

pub fn untyped(self) -> UntypedAccessor {
pub const fn untyped(self) -> UntypedAccessor {
UntypedAccessor::new(self.ref_fn, self.mut_fn)
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ pub struct UntypedAccessor {

impl UntypedAccessor {
/// Create a new type-erased accessor from a typed accessor pair.
pub fn new<S: 'static, T: 'static>(
pub const fn new<S: 'static, T: 'static>(
ref_fn: fn(&S) -> &T,
mut_fn: fn(&mut S) -> &mut T,
) -> Self {
Expand All @@ -133,7 +133,9 @@ impl UntypedAccessor {
///
/// Undefined behavior if `S` and `T` do not match the types used
/// when constructing this accessor.
pub unsafe fn typed_unchecked<S, T>(self) -> Accessor<S, T> {
pub const unsafe fn typed_unchecked<S, T>(
self,
) -> Accessor<S, T> {
unsafe {
Accessor {
ref_fn: core::mem::transmute::<*const (), fn(&S) -> &T>(
Expand Down
41 changes: 23 additions & 18 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<S, T> Field<S, T> {
}

/// Returns the raw field path string.
pub fn field_path(&self) -> &'static str {
pub const fn field_path(&self) -> &'static str {
self.field_path
}
}
Expand All @@ -78,7 +78,7 @@ where
T: 'static,
{
/// Converts into a [`UntypedField`] type.
pub fn untyped(&self) -> UntypedField {
pub const fn untyped(&self) -> UntypedField {
UntypedField::new::<S, T>(self.field_path)
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ pub struct UntypedField {
}

impl UntypedField {
pub fn new<S: 'static, T: 'static>(
pub const fn new<S: 'static, T: 'static>(
field_path: &'static str,
) -> Self {
Self {
Expand All @@ -182,42 +182,47 @@ impl UntypedField {
}
}

pub fn placeholder() -> Self {
pub const fn placeholder() -> Self {
Self::placeholder_with_path("$")
}

pub fn placeholder_with_path(field_path: &'static str) -> Self {
pub const fn placeholder_with_path(
field_path: &'static str,
) -> Self {
Self::new::<(), ()>(field_path)
}

/// Get the [`TypeId`] of the source type.
pub fn source_id(&self) -> TypeId {
pub const fn source_id(&self) -> TypeId {
self.source_id
}

/// Get the [`TypeId`] of the target type.
pub fn target_id(&self) -> TypeId {
pub const fn target_id(&self) -> TypeId {
self.target_id
}

/// See [`Field::field_path`].
pub fn field_path(&self) -> &'static str {
pub const fn field_path(&self) -> &'static str {
self.field_path
}

/// Converts into a typed [`Field<S, T>`].
///
/// # Panics
///
/// Panics if the type does not match.
pub fn typed<S: 'static, T: 'static>(self) -> Field<S, T> {
assert_eq!(TypeId::of::<S>(), self.source_id);
assert_eq!(TypeId::of::<T>(), self.target_id);
self.typed_unchecked()
/// Attempt to re-interpret this accessor as a typed [`Field`],
/// returning `None` if the [`TypeId`]s do not match.
pub fn typed<S: 'static, T: 'static>(
self,
) -> Option<Field<S, T>> {
if self.source_id == TypeId::of::<S>()
&& self.target_id == TypeId::of::<T>()
{
return Some(self.typed_unchecked());
}

None
}

/// Converts into a typed [`Field<S, T>`] without type checks.
pub fn typed_unchecked<S: 'static, T>(self) -> Field<S, T> {
pub const fn typed_unchecked<S: 'static, T>(self) -> Field<S, T> {
Field::new(self.field_path)
}
}
Expand Down