#Traits that accept self by-value if it has copy

13 messages · Page 1 of 1 (latest)

mighty mirage
#

So i have a trait, Attributed, which is used to indicate something has attributes associated with it within my programming language: ```rs

pub trait Attributed {
fn get_unbound_attr_checked<A>(
&self,
attr: A,
checked: &mut Vec<Value>,
) -> Result<Option<Value>>;

fn get_unbound_attr<A: Attribute>(&self, attr: A) -> Result<Option<Value>> { ... }
fn get_attr<A: Attribute>(&self, attr: A) -> Result<Option<Value>> { ... }
fn has_attr<A: Attribute>(&self, attr: A) -> Result<bool> { ... }
fn try_get_attr<A: Attribute>(&self, attr: A) -> Result<Value>
where
	Self: Clone + ToValue,
{ ... }

}

pub trait AttributedMut: Attributed {
fn get_unbound_attr_mut<A: Attribute>(&mut self, attr: A) -> Result<&mut Value>;
fn set_attr<A: Attribute>(&mut self, attr: A, value: Value) -> Result<()>;
fn del_attr<A: Attribute>(&mut self, attr: A) -> Result<Option<Value>>;
}


This trait's implemented by a few things, but the one i'm concerned about is `Value` itself, which is a 64-bit `Copy` type. Since these functions are called in a hot loop, having them take a reference for `Value`'s not ideal, as it's extra indirection. But I can't make the interface just by-value, because then nothing else could impelemnt it.

is there a good solution to this?
#

Ideally, something like ~const traits but for copy or reference would be lovely (ie if Self is copy, it takes self, if not &self), but im not sure

#

hm

#

one solution to this could be to just implement those things directly on Value itself but without taking a reference

fiery swallow
#

If you mark the functions with #[inline], they can be inlined across crates under the default profiles.

mighty mirage
#

yeah

#

maybe i profile it and possibly do #[inline(always)]

fiery swallow
#

If there's one thing the compiler must be good at, it's removing *& indirection.

mighty mirage
#

true

#

uh... weird

#

by doing all this, my code's become faster somehow

#

win? lol

mighty mirage
#

I figured it out @fiery swallow lol