Skip to content

Commit

Permalink
rune: Define and document more protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 1, 2024
1 parent 90d4a1a commit fa87ac7
Show file tree
Hide file tree
Showing 11 changed files with 786 additions and 181 deletions.
375 changes: 240 additions & 135 deletions crates/rune-core/src/protocol.rs

Large diffs are not rendered by default.

27 changes: 21 additions & 6 deletions crates/rune/src/compile/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl Context {
return_type: meta::DocType::new(ty.hash),
};

self.insert_native_fn(ty.hash, c, None)?;
self.insert_native_fn(&ty.type_info, ty.hash, c, None)?;
Some(signature)
}
None => None,
Expand Down Expand Up @@ -751,7 +751,7 @@ impl Context {
return_type: meta::DocType::new(ty.hash),
};

self.insert_native_fn(hash, c, variant.deprecated.as_deref())?;
self.insert_native_fn(&item, hash, c, variant.deprecated.as_deref())?;
Some(signature)
} else {
None
Expand Down Expand Up @@ -990,7 +990,7 @@ impl Context {

let signature = meta::Signature::from_context(&f.doc, &m.common)?;

self.insert_native_fn(m.hash, &f.handler, m.common.deprecated.as_deref())?;
self.insert_native_fn(&m.item, m.hash, &f.handler, m.common.deprecated.as_deref())?;

meta::Kind::Function {
associated: None,
Expand Down Expand Up @@ -1057,6 +1057,7 @@ impl Context {

let constructor = if let Some(constructor) = &variant.constructor {
self.insert_native_fn(
&internal_enum.static_type.type_info(),
variant_hash,
constructor,
variant.deprecated.as_deref(),
Expand Down Expand Up @@ -1174,10 +1175,20 @@ impl Context {
ConstValue::from(item.try_to_string()?),
)?;

self.insert_native_fn(*hash, &f.handler, assoc.common.deprecated.as_deref())?;
self.insert_native_fn(
&assoc.container_type_info,
*hash,
&f.handler,
assoc.common.deprecated.as_deref(),
)?;
}

self.insert_native_fn(hash, &f.handler, assoc.common.deprecated.as_deref())?;
self.insert_native_fn(
&assoc.container_type_info,
hash,
&f.handler,
assoc.common.deprecated.as_deref(),
)?;

meta::Kind::Function {
associated: Some(assoc.name.kind.try_clone()?),
Expand Down Expand Up @@ -1211,12 +1222,16 @@ impl Context {

fn insert_native_fn(
&mut self,
display: &dyn fmt::Display,
hash: Hash,
handler: &Arc<FunctionHandler>,
deprecation: Option<&str>,
) -> Result<(), ContextError> {
if self.functions.contains_key(&hash) {
return Err(ContextError::ConflictingFunction { hash });
return Err(ContextError::ConflictingFunction {
part: display.try_to_string()?.try_into()?,
hash,
});
}

self.functions.try_insert(hash, handler.clone())?;
Expand Down
8 changes: 6 additions & 2 deletions crates/rune/src/compile/context_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum ContextError {
name: &'static str,
},
ConflictingFunction {
part: Box<str>,
hash: Hash,
},
ConflictingFunctionName {
Expand Down Expand Up @@ -199,8 +200,11 @@ impl fmt::Display for ContextError {
ContextError::InternalAlreadyPresent { name } => {
write!(f, "Type for name `{name}` is already present")?;
}
ContextError::ConflictingFunction { hash } => {
write!(f, "Function with hash `{hash}` already exists")?;
ContextError::ConflictingFunction { part, hash } => {
write!(
f,
"Function with hash `{hash}` part of `{part}` already exists"
)?;
}
ContextError::ConflictingFunctionName { item, hash } => {
write!(f, "Function `{item}` already exists with hash `{hash}`")?;
Expand Down
4 changes: 2 additions & 2 deletions crates/rune/src/compile/v1/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,8 +1472,8 @@ fn expr_binary<'a, 'hir>(
ast::BinOp::Neq(..) => InstOp::Neq,
ast::BinOp::Lt(..) => InstOp::Lt,
ast::BinOp::Gt(..) => InstOp::Gt,
ast::BinOp::Lte(..) => InstOp::Lte,
ast::BinOp::Gte(..) => InstOp::Gte,
ast::BinOp::Lte(..) => InstOp::Le,
ast::BinOp::Gte(..) => InstOp::Ge,
ast::BinOp::As(..) => InstOp::As,
ast::BinOp::Is(..) => InstOp::Is,
ast::BinOp::IsNot(..) => InstOp::IsNot,
Expand Down
3 changes: 0 additions & 3 deletions crates/rune/src/doc/build/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub(super) struct Trait<'a> {
#[serde(serialize_with = "super::serialize_item")]
pub(super) item: &'a Item,
pub(super) hash: Hash,
pub(super) module: String,
pub(super) name: &'a str,
pub(super) url: RelativePathBuf,
pub(super) methods: Vec<Method<'a>>,
Expand Down Expand Up @@ -118,13 +117,11 @@ pub(super) fn build_assoc_fns<'m>(
.and_then(|c| c.as_str())
.context("Missing trait name")?;

let module = cx.module_path_html(meta, false)?;
let url = cx.item_path(item, ItemKind::Trait)?;

traits.try_push(Trait {
item,
hash,
module,
name,
url,
methods,
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/doc/static/type.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
{{#each traits}}
<div class="item item-trait">
<div id="trait.{{this.item}}" class="item-title">
impl {{literal this.module}}::<a href="{{this.url}}" class="trait">{{this.name}}</a>
impl <a href="{{this.url}}" class="trait">{{this.name}}</a>
for <span class="{{../what_class}}">{{../name}}</span>
</div>
</div>
Expand Down
Loading

0 comments on commit fa87ac7

Please sign in to comment.