Struct Table

Source
pub struct Table<K, E>
where K: Key + Ord + Borrow<<K as Key>::Borrowed>, <K as Key>::Borrowed: Ord + ToOwned<Owned = K>, E: Entry, E::Metadata: TableMetadata,
{ /* private fields */ }
Expand description

§A table exported to other plugins

An instance of this type can be exposed to other plugins via tables::TablesInput::add_table

The generic parameters are: key type and entry type. The key type is anything usable as a table key, while the entry type is a type that can be stored in the table. You can obtain such a type by #[derive]ing Entry on a struct describing all the table fields.

Supported key types include:

  • integer types (u8/i8, u16/i16, u32/i32, u64/i64)
  • crate::tables::import::Bool (an API equivalent of bool)
  • &CStr (spelled as just CStr when used as a generic argument)

See crate::tables::export for details.

The implementation is thread-safe when the thread-safe-tables feature is enabled.

Implementations§

Source§

impl<K, E> Table<K, E>
where K: Key + Ord + Borrow<<K as Key>::Borrowed>, <K as Key>::Borrowed: Ord + ToOwned<Owned = K>, E: Entry, E::Metadata: TableMetadata,

Source

pub fn new_with_metadata( tag: &'static CStr, metadata: &RefShared<ExtensibleEntryMetadata<<E as HasMetadata>::Metadata>>, ) -> Result<Self, Error>

Create a new table using provided metadata

This is only expected to be used by the derive macro.

Source

pub fn new(name: &'static CStr) -> Result<Self, Error>

Create a new table

Source

pub fn data(&self) -> RefShared<BTreeMap<K, RefShared<ExtensibleEntry<E>>>>

Get an accessor to the underlying data

This method returns a reference to the underlying BTreeMap, containing all the table’s data. It can be useful for:

  • accessing the table from a different thread (with the thread-safe-tables feature enabled)
  • bypassing the table API for convenience or more control over locking

To actually access the BTreeMap, you first need to lock the returned object for reading (data.read()) or writing (data.write()).

Source

pub fn name(&self) -> &'static CStr

Return the table name.

Source

pub fn size(&self) -> usize

Return the number of entries in the table.

Source

pub fn lookup<Q>( &self, key: &Q, ) -> Option<ArcRwLockWriteGuard<RawRwLock, ExtensibleEntry<E>>>
where K: Borrow<Q>, Q: Ord + ?Sized,

Get an entry corresponding to a particular key.

Source

pub fn get_field_value( &self, entry: &ArcRwLockWriteGuard<RawRwLock, ExtensibleEntry<E>>, field: &FieldDescriptor, out: &mut ss_plugin_state_data, ) -> Result<(), Error>

Get the value for a field in an entry.

Source

pub fn iterate_entries<F>(&mut self, func: F) -> bool
where F: FnMut(&mut ArcRwLockWriteGuard<RawRwLock, ExtensibleEntry<E>>) -> bool,

Execute a closure on all entries in the table with read-only access.

The iteration continues until all entries are visited or the closure returns false.

Source

pub fn clear(&mut self)

Remove all entries from the table.

Source

pub fn erase<Q>( &mut self, key: &Q, ) -> Option<ArcRwLockWriteGuard<RawRwLock, ExtensibleEntry<E>>>
where K: Borrow<Q>, Q: Ord + ?Sized,

Erase an entry by key.

Source

pub fn create_entry( &self, ) -> Result<ArcRwLockWriteGuard<RawRwLock, ExtensibleEntry<E>>, Error>

Create a new table entry.

This is a detached entry that can be later inserted into the table using Table::insert.

Source

pub fn create_entry_fn( &self, ) -> impl Fn() -> Result<RefShared<ExtensibleEntry<E>>, Error>

Return a closure for creating table entries

The Table object itself cannot be shared between threads safely even with the thread-safe-tables feature enabled, but almost full functionality can be achieved using two objects that can:

  1. The underlying BTreeMap, obtained from Table::data
  2. A closure capable of creating a new entry (returned from this function)

The only functionality missing is listing table fields, and until a use case comes along, it’s likely to remain unimplemented.

The entry obtained by calling the closure returned from create_entry_fn can be later inserted into the table e.g. by calling BTreeMap::insert.

To actually access the entry’s fields, you first need to lock the returned object for reading (data.read()) or writing (data.write()).

Source

pub fn insert<Q>( &mut self, key: &Q, entry: ArcRwLockWriteGuard<RawRwLock, ExtensibleEntry<E>>, ) -> Option<ArcRwLockWriteGuard<RawRwLock, ExtensibleEntry<E>>>
where K: Borrow<Q>, Q: Ord + ToOwned<Owned = K> + ?Sized,

Attach an entry to a table key

Source

pub fn write( &self, entry: &mut ArcRwLockWriteGuard<RawRwLock, ExtensibleEntry<E>>, field: &FieldDescriptor, value: &ss_plugin_state_data, ) -> Result<(), Error>

Write a value to a field of an entry

Source

pub fn list_fields(&mut self) -> &[ss_plugin_table_fieldinfo]

Return a list of fields as a slice of raw FFI objects

Source

pub fn get_field( &self, name: &CStr, field_type: FieldTypeId, ) -> Option<FieldRef>

Return a field descriptor for a particular field

The requested field_type must match the actual type of the field

Source

pub fn add_field( &mut self, name: &CStr, field_type: FieldTypeId, read_only: bool, ) -> Option<FieldRef>

Add a new field to the table

Trait Implementations§

Source§

impl<K, E> Debug for Table<K, E>
where K: Key + Ord + Debug + Borrow<<K as Key>::Borrowed>, <K as Key>::Borrowed: Ord + ToOwned<Owned = K>, E: Entry + Debug, E::Metadata: TableMetadata + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, E> !Freeze for Table<K, E>

§

impl<K, E> !RefUnwindSafe for Table<K, E>

§

impl<K, E> !Send for Table<K, E>

§

impl<K, E> !Sync for Table<K, E>

§

impl<K, E> Unpin for Table<K, E>
where <E as HasMetadata>::Metadata: Sized,

§

impl<K, E> !UnwindSafe for Table<K, E>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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.