KBox

Struct KBox 

Source
pub struct KBox<T: KObject> { /* private fields */ }
Expand description

Represents a memory managed K pointer. They are the KDB equivalent of a Rust Box, a zero overhead wrapper around a K pointer. It will call r0 to decrement the reference count when it is dropped.

Implementations§

Source§

impl<T: KValue> KBox<Atom<T>>

Source

pub fn new_atom(value: T) -> KBox<Atom<T>>

Creates a new atom with the specified value.

Examples found in repository?
examples/using_atoms.rs (line 6)
5fn main() {
6    let mut k = KBox::new_atom(42u8);
7    println!("{}", k);
8    assert_eq!(k.value(), 42);
9
10    k.set_value(43);
11
12    assert_eq!(k.value(), 43);
13}
More examples
Hide additional examples
examples/using_any.rs (line 4)
3fn main() {
4    let int = KBox::new_atom(42);
5
6    // convert to an "any" value:
7    let any: KBox<Any> = int.into();
8
9    // convert back to an i32 atom.
10    let int = cast!(any; Atom<i32>);
11    println!("{:?}", int);
12
13    let any: KBox<Any> = int.into();
14    // try to convert to a u8 atom. This will fail!
15    if let Err(e) = try_cast!(any; Atom<u8>) {
16        println!("Error: {}", e);
17    }
18}
examples/fun_with_symbols.rs (line 12)
3fn main() {
4    //Create two identical symbols in different ways, and check that they are equal.
5    let sym = symbol("Hello, World");
6    // Note: converting a string into a symbol is not an infallible operation
7    // rust strings can contain embedded nuls, whereas symbols cannot.
8    let sym_2 = Symbol::new(String::from("Hello") + ", World").unwrap();
9    assert_eq!(sym, sym_2);
10
11    // As an atom:
12    let atom = KBox::new_atom(sym);
13    let atom_2 = KBox::new_atom(Symbol::new(String::from("Hello") + ", World").unwrap());
14
15    assert_eq!(atom.value(), atom_2.value());
16
17    // Note that because rust strings are utf-8, and symbols have no encoding requirement,
18    // this may not display the same way as you will see it in kdb, especially if the string is
19    // not a valid ASCII or utf-8 string.
20    println!("{}", sym);
21}
Source§

impl KBox<Dictionary>

Source

pub fn new_dict() -> Self

Create a new empty dictionary.

Examples found in repository?
examples/using_dictionaries.rs (line 6)
4fn main() {
5    //Symbols use a lot of try_into. Sad.
6    let mut dict = KBox::new_dict();
7
8    dict.insert(symbol("One"), 1i32);
9    dict.insert(symbol("Two"), 2i32);
10    dict.insert(symbol("Three"), 3i32);
11
12    println!("{:?}", cast!(&dict[symbol("Two")]; Atom<Symbol>));
13}
Source§

impl KBox<KError>

Source

pub fn new_error(msg: &str) -> Self

Create a new KDB error from the specified string.

Source§

impl<T: KObject> KBox<T>

Source

pub fn into_raw(self) -> *mut T

Converts a box into a raw unmanged K pointer. Note that into raw will consume the KBox, and not call r0, so it’s possible to leak memory by doing this.

Source

pub unsafe fn from_raw(k: *mut K) -> Self

Converts a raw K pointer into a boxed K object. This is the reciprocal of into_raw

§Safety

The type of the k pointer must match the type of the KBox being used. The pointer must not be null.

Do not use this to take ownership of a kdb callback function parameter, use from_shared instead.

Source

pub unsafe fn from_shared(t: &mut T) -> Self

Takes a reference and calls r1, incrementing the reference count and “re-boxing” it. Typically this is a bad thing as you have multiple owned references and kdb does not provide equivalent guarantees to rust about what happens to shared references (especially when reallocating a list for example)

However in the embedded case, where you do not own the parameter and you wish to manipulate it without copying the data, then you need this functionality.

§Safety

A reference should not be owned by more than one KBox instance.

Source§

impl<T: KListable> KBox<List<T>>

Source

pub fn new_list() -> Self

Creates a new empty list.

Note that if you are converting a rust collection to a list, collect is a more efficient. If you are creating a list with a set of known elements, use the list! macro.

Examples found in repository?
examples/creating_a_list.rs (line 8)
3fn main() {
4    //Create a list containing the numbers from 1 to 10.
5    let mut list: KBox<List<u8>> = (1..=10).collect();
6
7    //Create another list by pushing incrementally
8    let mut list_2: KBox<List<u8>> = KBox::new_list();
9    for i in 11..=20 {
10        list_2.push(i);
11    }
12
13    //Append the second list to the first
14    list.join(list_2);
15
16    // Append from an iterator:
17    list.extend(21..=30);
18
19    // write out the contents
20    for i in list.iter() {
21        println!("{}", i);
22    }
23
24    // we can also use it as a slice:
25    for i in &list[..5] {
26        println!("{}", i)
27    }
28}
Source

pub fn join(&mut self, list: KBox<List<T>>)

Appends a list to this one, consuming it and adding it’s elements to the new one.

Examples found in repository?
examples/creating_a_list.rs (line 14)
3fn main() {
4    //Create a list containing the numbers from 1 to 10.
5    let mut list: KBox<List<u8>> = (1..=10).collect();
6
7    //Create another list by pushing incrementally
8    let mut list_2: KBox<List<u8>> = KBox::new_list();
9    for i in 11..=20 {
10        list_2.push(i);
11    }
12
13    //Append the second list to the first
14    list.join(list_2);
15
16    // Append from an iterator:
17    list.extend(21..=30);
18
19    // write out the contents
20    for i in list.iter() {
21        println!("{}", i);
22    }
23
24    // we can also use it as a slice:
25    for i in &list[..5] {
26        println!("{}", i)
27    }
28}
Source

pub fn push(&mut self, item: T::ListItem)

Appends an element to the end of the list.

Examples found in repository?
examples/creating_a_list.rs (line 10)
3fn main() {
4    //Create a list containing the numbers from 1 to 10.
5    let mut list: KBox<List<u8>> = (1..=10).collect();
6
7    //Create another list by pushing incrementally
8    let mut list_2: KBox<List<u8>> = KBox::new_list();
9    for i in 11..=20 {
10        list_2.push(i);
11    }
12
13    //Append the second list to the first
14    list.join(list_2);
15
16    // Append from an iterator:
17    list.extend(21..=30);
18
19    // write out the contents
20    for i in list.iter() {
21        println!("{}", i);
22    }
23
24    // we can also use it as a slice:
25    for i in &list[..5] {
26        println!("{}", i)
27    }
28}

Trait Implementations§

Source§

impl<T: KValue> AsRef<Any> for KBox<Atom<T>>

Source§

fn as_ref(&self) -> &Any

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Any> for KBox<Dictionary>

Source§

fn as_ref(&self) -> &Any

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: KListable> AsRef<Any> for KBox<List<T>>

Source§

fn as_ref(&self) -> &Any

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Any> for KBox<Table>

Source§

fn as_ref(&self) -> &Any

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: KObject> AsRef<T> for KBox<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: KObject + Debug> Debug for KBox<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: KObject> Deref for KBox<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: KObject> DerefMut for KBox<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T: KObject + Display> Display for KBox<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: KObject> Drop for KBox<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: KListable> Extend<<T as KListable>::ListItem> for KBox<List<T>>

Source§

fn extend<I: IntoIterator<Item = T::ListItem>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: KValue> From<KBox<Atom<T>>> for KBox<Any>

Source§

fn from(value: KBox<Atom<T>>) -> Self

Converts to this type from the input type.
Source§

impl From<KBox<Dictionary>> for KBox<Any>

Source§

fn from(value: KBox<Dictionary>) -> Self

Converts to this type from the input type.
Source§

impl From<KBox<KError>> for Error

Source§

fn from(ke: KBox<KError>) -> Self

Converts to this type from the input type.
Source§

impl From<KBox<KError>> for KBox<Any>

Source§

fn from(value: KBox<KError>) -> Self

Converts to this type from the input type.
Source§

impl<T: KListable> From<KBox<List<T>>> for KBox<Any>

Source§

fn from(value: KBox<List<T>>) -> Self

Converts to this type from the input type.
Source§

impl From<KBox<Table>> for KBox<Any>

Source§

fn from(value: KBox<Table>) -> Self

Converts to this type from the input type.
Source§

impl<T: KValue> From<T> for KBox<Any>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: KValue> From<T> for KBox<Atom<T>>

Source§

fn from(val: T) -> KBox<Atom<T>>

Converts to this type from the input type.
Source§

impl<T: KListable> FromIterator<<T as KListable>::ListItem> for KBox<List<T>>

Source§

fn from_iter<I: IntoIterator<Item = T::ListItem>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash + KObject> Hash for KBox<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord + KObject> Ord for KBox<T>

Source§

fn cmp(&self, other: &KBox<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq + KObject> PartialEq for KBox<T>

Source§

fn eq(&self, other: &KBox<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd + KObject> PartialOrd for KBox<T>

Source§

fn partial_cmp(&self, other: &KBox<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq + KObject> Eq for KBox<T>

Source§

impl<T: KObject> StructuralPartialEq for KBox<T>

Auto Trait Implementations§

§

impl<T> Freeze for KBox<T>

§

impl<T> RefUnwindSafe for KBox<T>
where T: RefUnwindSafe,

§

impl<T> !Send for KBox<T>

§

impl<T> !Sync for KBox<T>

§

impl<T> Unpin for KBox<T>

§

impl<T> UnwindSafe for KBox<T>
where T: RefUnwindSafe,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.