Skip to content

Commit

Permalink
rune: Use empty Hash as placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jul 22, 2024
1 parent 9e0d80d commit 5865368
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 33 deletions.
10 changes: 10 additions & 0 deletions crates/rune-core/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ impl Hash {
Self(hash).with_type_parameters(parameters)
}

/// Return the current hash if it is non-empty.
#[inline]
pub fn as_non_empty(&self) -> Option<Self> {
if self.is_empty() {
None
} else {
Some(*self)
}
}

/// Coerce a hash into its inner numerical value.
#[doc(hidden)]
pub const fn into_inner(self) -> u64 {
Expand Down
4 changes: 2 additions & 2 deletions crates/rune-macros/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ where
#[inline]
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
#meta::DocType::with_generics(
Some(<Self as #type_of>::type_hash()),
<Self as #type_of>::type_hash(),
[#(<#generic_names as #maybe_type_of>::maybe_type_of()?),*]
)
}
Expand All @@ -632,7 +632,7 @@ where
#[inline]
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
#meta::DocType::with_generics(
Some(<Self as #type_of>::type_hash()),
<Self as #type_of>::type_hash(),
[#(<#generic_names as #maybe_type_of>::maybe_type_of()),*]
)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/rune/src/compile/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ impl Context {
#[cfg(feature = "doc")]
arguments: Some(fields_to_arguments(fields)?),
#[cfg(feature = "doc")]
return_type: meta::DocType::new(Some(ty.hash)),
return_type: meta::DocType::new(ty.hash),
};

self.insert_native_fn(hash, c, None)?;
Expand Down Expand Up @@ -510,7 +510,7 @@ impl Context {
#[cfg(feature = "doc")]
arguments: Some(fields_to_arguments(fields)?),
#[cfg(feature = "doc")]
return_type: meta::DocType::new(Some(ty.hash)),
return_type: meta::DocType::new(ty.hash),
};

self.insert_native_fn(hash, c, variant.deprecated.as_deref())?;
Expand Down Expand Up @@ -712,7 +712,7 @@ impl Context {
#[cfg(feature = "doc")]
arguments: Some(fields_to_arguments(fields)?),
#[cfg(feature = "doc")]
return_type: meta::DocType::new(Some(hash)),
return_type: meta::DocType::new(hash),
})
} else {
None
Expand Down Expand Up @@ -903,7 +903,7 @@ fn fields_to_arguments(fields: &Fields) -> alloc::Result<Box<[meta::DocArgument]
for &name in fields {
out.try_push(meta::DocArgument {
name: meta::DocName::Name(Box::try_from(name)?),
base: None,
base: Hash::EMPTY,
generics: Box::default(),
})?;
}
Expand All @@ -916,7 +916,7 @@ fn fields_to_arguments(fields: &Fields) -> alloc::Result<Box<[meta::DocArgument]
for n in 0..args {
out.try_push(meta::DocArgument {
name: meta::DocName::Index(n),
base: None,
base: Hash::EMPTY,
generics: Box::default(),
})?;
}
Expand Down
10 changes: 5 additions & 5 deletions crates/rune/src/compile/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub(crate) struct DocArgument {
/// The name of an argument.
pub(crate) name: DocName,
/// The base type.
pub(crate) base: Option<Hash>,
pub(crate) base: Hash,
/// Generic parameters.
pub(crate) generics: Box<[DocType]>,
}
Expand All @@ -376,7 +376,7 @@ pub(crate) struct DocArgument {
pub struct DocType {
/// The base type.
#[cfg(feature = "doc")]
pub(crate) base: Option<Hash>,
pub(crate) base: Hash,
/// Generic parameters.
#[cfg(feature = "doc")]
pub(crate) generics: Box<[DocType]>,
Expand All @@ -386,7 +386,7 @@ impl DocType {
/// Construct type documentation.
#[cfg_attr(not(feature = "doc"), allow(unused_variables))]
pub fn with_generics<const N: usize>(
base: Option<Hash>,
base: Hash,
generics: [DocType; N],
) -> alloc::Result<Self> {
Ok(Self {
Expand All @@ -398,7 +398,7 @@ impl DocType {
}

#[cfg(feature = "doc")]
pub(crate) fn new(base: Option<Hash>) -> Self {
pub(crate) fn new(base: Hash) -> Self {
Self {
base,
generics: Box::default(),
Expand All @@ -408,7 +408,7 @@ impl DocType {
pub(crate) fn empty() -> Self {
Self {
#[cfg(feature = "doc")]
base: None,
base: Hash::EMPTY,
#[cfg(feature = "doc")]
generics: Box::default(),
}
Expand Down
14 changes: 6 additions & 8 deletions crates/rune/src/doc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ impl<'m> Ctxt<'_, 'm> {
write!(o, "<")?;

while let Some(ty) = it.next() {
match ty.base {
match ty.base.as_non_empty() {
Some(hash) => {
self.write_link(o, hash, None, &ty.generics)?;
}
Expand Down Expand Up @@ -647,15 +647,15 @@ impl<'m> Ctxt<'_, 'm> {

while let Some(arg) = it.next() {
if matches!(sig, Signature::Instance) && arg.name.is_self() {
if let Some(hash) = arg.base {
if let Some(hash) = arg.base.as_non_empty() {
self.write_link(&mut string, hash, Some("self"), &[])?;
} else {
write!(string, "self")?;
}
} else {
write!(string, "{}", arg.name)?;

if let Some(hash) = arg.base {
if let Some(hash) = arg.base.as_non_empty() {
string.try_push_str(": ")?;
self.write_link(&mut string, hash, None, &arg.generics)?;
}
Expand Down Expand Up @@ -1110,11 +1110,9 @@ fn build_function<'m>(cx: &mut Ctxt<'_, 'm>, meta: Meta<'m>) -> Result<Builder<'
let doc = cx.render_docs(meta, meta.docs, true)?;

let return_type = match f.return_type {
meta::DocType {
base: Some(hash),
generics,
..
} => Some(cx.link(*hash, None, generics)?),
meta::DocType { base, generics, .. } if !base.is_empty() => {
Some(cx.link(*base, None, generics)?)
}
_ => None,
};

Expand Down
16 changes: 6 additions & 10 deletions crates/rune/src/doc/build/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ pub(super) fn build_assoc_fns<'m>(
args: cx.args_to_string(sig, assoc.arguments)?,
parameters,
return_type: match assoc.return_type {
meta::DocType {
base: Some(hash),
generics,
..
} => Some(cx.link(*hash, None, generics)?),
meta::DocType { base, generics, .. } if !base.is_empty() => {
Some(cx.link(*base, None, generics)?)
}
_ => None,
},
line_doc,
Expand Down Expand Up @@ -143,11 +141,9 @@ pub(super) fn build_assoc_fns<'m>(
field,
repr,
return_type: match assoc.return_type {
meta::DocType {
base: Some(hash),
generics,
..
} => Some(cx.link(*hash, None, generics)?),
meta::DocType { base, generics, .. } if !base.is_empty() => {
Some(cx.link(*base, None, generics)?)
}
_ => None,
},
doc,
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ macro_rules! impl_static_type {
#[inline]
fn maybe_type_of() -> $crate::alloc::Result<$crate::compile::meta::DocType> {
$crate::compile::meta::DocType::with_generics(
Some(<$ty as $crate::runtime::TypeOf>::type_hash()),
<$ty as $crate::runtime::TypeOf>::type_hash(),
[$(<$p as $crate::runtime::MaybeTypeOf>::maybe_type_of()?),*]
)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rune/src/languageserver/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub(super) fn complete_native_instance_data(
let return_type = signature
.return_type
.base
.as_non_empty()
.and_then(|hash| context.lookup_meta_by_hash(hash).next())
.and_then(|r| r.item.as_deref());

Expand Down Expand Up @@ -177,6 +178,7 @@ pub(super) fn complete_native_loose_data(
let return_type = signature
.return_type
.base
.as_non_empty()
.and_then(|hash| context.lookup_meta_by_hash(hash).next())
.and_then(|r| r.item.as_deref());

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/query/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ impl<'a, 'arena> Query<'a, 'arena> {

out.try_push(meta::DocArgument {
name,
base: None,
base: Hash::EMPTY,
generics: Box::default(),
})?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/tests/bug_344.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl TypeOf for GuardCheck {
impl MaybeTypeOf for GuardCheck {
#[inline]
fn maybe_type_of() -> alloc::Result<meta::DocType> {
Ok(meta::DocType::new(Some(<Self as TypeOf>::type_hash())))
Ok(meta::DocType::new(<Self as TypeOf>::type_hash()))
}
}

Expand Down

0 comments on commit 5865368

Please sign in to comment.