pub trait Native: Debug {
// Required methods
fn type_name(&self) -> &str;
fn repr(&self) -> String;
fn as_any(&self) -> &dyn Any;
// Provided methods
fn display(&self) -> String { ... }
fn truthy(&self) -> bool { ... }
fn walking(&self) -> bool { ... }
fn equals(&self, _other: &dyn Native) -> bool { ... }
fn hash(&self) -> Option<i64> { ... }
}Expand description
A value whose type lives above this crate.
Implementors are runtime objects: builtin functions, iterators, exception instances. They answer the questions any value has to answer and keep the rest to themselves.
Required Methods§
Sourcefn type_name(&self) -> &str
fn type_name(&self) -> &str
What type(x).__name__ says, which is what error messages need.
Borrowed from the value rather than &'static str, because an instance
of a class defined in Python has to name that class and the name belongs
to the class object.
Sourcefn repr(&self) -> String
fn repr(&self) -> String
What repr prints.
CPython puts an address in most of these, as in
<built-in function print> for one that has no address to show and
<list_iterator object at 0x102f9c130> for one that does. An address is
not reproducible between runs, so nothing may depend on the exact text.
Provided Methods§
Sourcefn display(&self) -> String
fn display(&self) -> String
What str prints, which for a type with no __str__ of its own is
whatever repr prints. Almost every native type wants the default. An
exception is the one that does not, because str(e) is the message and
repr(e) is the call that would make it again.
Sourcefn truthy(&self) -> bool
fn truthy(&self) -> bool
Python’s truth protocol, which for an object with no __bool__ and no
__len__ is true. Almost every native type wants the default.
Sourcefn walking(&self) -> bool
fn walking(&self) -> bool
Whether this is already an iterator, meaning iter(x) is x and a for
over a half consumed one carries on rather than starting again.
It is a question rather than a downcast because the layer above has several types that answer yes, and they arrive one at a time. Asking each of them in turn would put a list in the one function that turns a value into a walk, and that list would grow by a line every time a lazy builtin is written.
Sourcefn equals(&self, _other: &dyn Native) -> bool
fn equals(&self, _other: &dyn Native) -> bool
Whether this is equal to another native value that is not the same object as it.
Identity is answered before this is asked, so the default is no, which
is what an object with no __eq__ gets. The types that override it are
the ones a lookup builds afresh every time: a.f == a.f is true in
Python even though a.f is a.f is false, because a bound method is
equal to another one wrapping the same function and the same receiver.
Sourcefn hash(&self) -> Option<i64>
fn hash(&self) -> Option<i64>
This value’s hash, or nothing to be hashed by address like an object
with no __hash__ of its own.
A type that overrides Native::equals with anything other than
identity has to override this too, because two values that are equal
and hash differently get filed in one slot of a dictionary and looked
for in another, and what a program sees is a key it just put in coming
back missing.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".