Skip to main content

Object

Enum Object 

Source
pub enum Object {
Show 14 variants None, NotImplemented, Ellipsis, Bool(bool), Int(Int), Float(f64), Str(Rc<Str>), Bytes(Rc<[u8]>), Tuple(Rc<[Object]>), List(Rc<RefCell<Vec<Object>>>), Dict(Rc<RefCell<Dict>>), Set(Rc<RefCell<Set>>), Slice(Rc<Slice>), Native(Rc<dyn Native>),
}
Expand description

A Python value.

Variants§

§

None

None.

§

NotImplemented

The answer an operator gives when it does not know how, which is what lets Python try the reflected one before giving up.

§

Ellipsis

..., which is a value as well as a piece of syntax.

§

Bool(bool)

True or False, which is an int in Python and is kept apart here because repr and type both need to know which one it is.

§

Int(Int)

An int, of any size.

§

Float(f64)

A float, which is an IEEE double and nothing more.

§

Str(Rc<Str>)

A str, which is a sequence of code points.

§

Bytes(Rc<[u8]>)

A bytes, which is a sequence of bytes and never equal to a str.

§

Tuple(Rc<[Object]>)

A tuple, which cannot change and so holds its elements inline.

§

List(Rc<RefCell<Vec<Object>>>)

A list, which can.

§

Dict(Rc<RefCell<Dict>>)

A dict, which remembers the order things were put into it.

§

Set(Rc<RefCell<Set>>)

A set, which does not.

§

Slice(Rc<Slice>)

A slice, which is what a:b:c inside a subscript builds. It holds three objects rather than three integers, because the numbers only have to be numbers at the point a sequence uses them.

§

Native(Rc<dyn Native>)

A value whose type is defined above this crate, which is how the runtime gets functions, iterators and exceptions without this crate having to know what any of those are. See Native.

Implementations§

Source§

impl Object

Source

pub const fn int(value: i64) -> Self

An integer from a machine word.

Source

pub fn str(value: impl Into<Str>) -> Self

A string from Rust text.

Source

pub fn list(items: Vec<Object>) -> Self

A list from its elements.

Source

pub fn tuple(items: Vec<Object>) -> Self

A tuple from its elements.

Source

pub fn dict(entries: Dict) -> Self

A dict.

Source

pub fn set(members: Set) -> Self

A set.

Source

pub fn native(value: impl Native + 'static) -> Self

A value of a type defined above this crate.

Source

pub fn downcast<T: Native + 'static>(&self) -> Option<&T>

The native value inside this object, if that is what it is and if it is the type asked for.

This is the downcast the runtime uses to find out whether the thing in a register is the kind of object it can call or step.

Source

pub fn exception(&self) -> Option<&Exception>

This value as the exception it is, or None if it is not one.

The one downcast common enough to be worth a name, since raise, except and the traceback printer all ask the same question.

Source

pub fn type_name(&self) -> &str

What type(x).__name__ says, which is what every error message needs.

Borrowed rather than &'static str, because a class defined in Python names itself and that name is owned by the class object. Everything built in still hands back a literal.

Source

pub fn truthy(&self) -> bool

Python’s truth protocol for the types that have no __bool__ to run.

Zero of any numeric type is false, an empty container is false, None is false, and everything else is true. When user-defined types arrive this becomes the thing that calls __bool__ and then __len__, and the answers below become what the builtin types answer with.

Source

pub fn is(&self, other: &Self) -> bool

Whether these are the same object, which is what is asks.

For a heap value it is the pointer. For an immediate it is the value, which is the one place this differs from CPython in a way a program could see: x = 1000; y = 1000; x is y is False in CPython because there are two objects, and is True here because there are none. The tagged representation makes that true for real, and the language does not promise either answer.

The loudest case of it is the NaN, since identity is what decides nan in [nan] and whether a NaN can be found in a dict again. Two separately made ones are two objects in CPython and one value here.

Source

pub fn equals(&self, other: &Self) -> bool

What == answers.

Numbers compare across their types, so 1 == 1.0 == True, and an integer too large for a float still gets an exact answer. Everything else compares only within its own type: a str is never equal to the bytes that spell it and a tuple is never equal to a list, however alike either pair looks.

A container holding itself sends this into a recursion CPython turns into a RecursionError. There is no recursion limit here yet, because there are no frames to count, and it arrives with them.

Source

pub fn same_value(&self, other: &Self) -> bool

What a container asks about its elements, and what a dict asks about a key, which is x is y or x == y rather than plain ==.

The identity half is not an optimization. x == x is false for a NaN, so [nan] == [nan] would be false without it where CPython says true for the same NaN in both, and a NaN stored in a dict could never be found again.

Source

pub fn repr(&self) -> String

What repr prints.

Source

pub fn display(&self) -> String

What str prints, which differs from repr only for a string itself.

print('a') writes a and print(['a']) writes ['a'], because a container prints its elements with repr however it was printed itself.

Trait Implementations§

Source§

impl Clone for Object

Source§

fn clone(&self) -> Object

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Object

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Object

§

impl !Send for Object

§

impl !Sync for Object

§

impl !UnwindSafe for Object

§

impl Freeze for Object

§

impl Unpin for Object

§

impl UnsafeUnpin for Object

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.