Implementation:LaurentMazare Tch rs Scalar
| Knowledge Sources | |
|---|---|
| Domains | Scalar Values, FFI, Type Conversion |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
The Scalar struct wraps a PyTorch C++ scalar value (C_scalar) with safe constructors, accessors, bidirectional From conversions for i64 and f64, and proper memory management via Drop.
Description
The Scalar struct holds a raw pointer to a torch_sys::C_scalar, representing a single numeric value in the PyTorch C++ runtime. It serves as the Rust-side wrapper for passing scalar arguments to tensor operations.
Two constructors are provided:
- Scalar::int(v: i64) creates an integer scalar via torch_sys::ats_int
- Scalar::float(v: f64) creates a floating-point scalar via torch_sys::ats_float
Three accessors extract values:
- to_int(&self) -> Result<i64, TchError> extracts as integer via ats_to_int
- to_float(&self) -> Result<f64, TchError> extracts as float via ats_to_float
- to_string(&self) -> Result<String, TchError> gets a string representation via ats_to_string, using the ptr_to_string utility for C string conversion
The Debug trait formats the scalar as scalar<value> or err if string conversion fails.
The Drop trait ensures the underlying C scalar is freed via torch_sys::ats_free.
Six From trait implementations provide bidirectional conversion:
- From<i64> for Scalar and From<f64> for Scalar create scalars from Rust primitives
- From<Scalar> for i64 and From<Scalar> for f64 consume the scalar and extract a value
- From<&Scalar> for i64 and From<&Scalar> for f64 borrow the scalar and extract a value
Usage
Use Scalar when passing scalar values to PyTorch tensor operations that expect a Scalar parameter. The From implementations allow seamless conversion from Rust numeric types.
Code Reference
Source Location
- Repository: LaurentMazare_Tch_rs
- File: src/wrappers/scalar.rs
Signature
pub struct Scalar {
pub(super) c_scalar: *mut torch_sys::C_scalar,
}
impl Scalar {
pub fn int(v: i64) -> Scalar;
pub fn float(v: f64) -> Scalar;
pub fn to_int(&self) -> Result<i64, TchError>;
pub fn to_float(&self) -> Result<f64, TchError>;
pub fn to_string(&self) -> Result<String, TchError>;
}
impl Debug for Scalar { ... }
impl Drop for Scalar { ... }
impl From<i64> for Scalar { ... }
impl From<f64> for Scalar { ... }
impl From<Scalar> for i64 { ... }
impl From<Scalar> for f64 { ... }
impl From<&Scalar> for i64 { ... }
impl From<&Scalar> for f64 { ... }
Import
use tch::Scalar;
I/O Contract
| Method | Input | Output | Notes |
|---|---|---|---|
| Scalar::int | i64 | Scalar | Creates integer scalar |
| Scalar::float | f64 | Scalar | Creates float scalar |
| to_int | &self | Result<i64, TchError> | Truncates floats to integer |
| to_float | &self | Result<f64, TchError> | Promotes integers to float |
| to_string | &self | Result<String, TchError> | Returns TchError::Kind on null pointer |
| From Conversion | Direction | Behavior |
|---|---|---|
| i64 -> Scalar | Rust to C++ | Calls Scalar::int |
| f64 -> Scalar | Rust to C++ | Calls Scalar::float |
| Scalar -> i64 | C++ to Rust | Calls to_int().unwrap() |
| Scalar -> f64 | C++ to Rust | Calls to_float().unwrap() |
| &Scalar -> i64 | C++ to Rust (borrow) | Calls to_int().unwrap() |
| &Scalar -> f64 | C++ to Rust (borrow) | Calls to_float().unwrap() |
Usage Examples
use tch::Scalar;
// Create scalars
let pi = Scalar::float(std::f64::consts::PI);
let answer = Scalar::int(42);
// Extract values
assert_eq!(i64::from(&pi), 3); // truncated
assert_eq!(f64::from(&pi), std::f64::consts::PI);
assert_eq!(i64::from(&answer), 42);
assert_eq!(f64::from(&answer), 42.0);
// Debug formatting
println!("{:?}", pi); // scalar<3.14159>
println!("{:?}", answer); // scalar<42>
// Implicit conversion via From
let s: Scalar = 1337i64.into();
let s: Scalar = 3.14f64.into();